Code Like A Viking - programming tips II

Programming tips II.

Note: all code snippets are written in Python

No matter what language you choose, the basics are the same everywhere and there is no way how you can make it further to the “highschool” without going through the “elementary school” first. Even if you somehow make it, you’ll hit the wall later anyway and this time it will hurt. So let’s take a look at what are these basics in programming.

 

Variables

What is a variable? It sounds like some weird incantation. But it’s quite simple. A variable is something that didn’t exist before and by giving it a name you bring it from nothingness to life and from this moment on it “lives” and can be worked with. It’s like to be a god or a wizzard with an ability to create spells. In the computer language you make variables like this:

 

character = “Erik The Red”

my_favourite_weapon = “Viking Axe”

number_of_my_ships = 15

my_clan_loyalty = True

 

And from now on there is a character called Erik The Red, who’s favourite weapon is a Viking Axe, coming with his 15 ships and people loyal to him.

In other words you created a variable called character and put “Erik The Red” inside of it. So whenever you want to work with this variable you call its name (e.g. character or my_favourite_weapon) and order what’s gonna happen. So what orders can you give to a variable? You may want to print it to get what is inside:

 

print(character)

 

result:

Erik The Red

 

Or you may decide to change what’s inside your variable:

 

character = “Ragnar Lothbrok”

number_of_my_ships = 120

 

print(character)

print(number_of_my_ships)

 

result:

Ragnar Lothbrok

120

 

All of a sudden you got Ragnar Lothbrok, with a decent fleet which is numerous enough to attack Paris – which happend in 845 A.D. Isn’t life beautiful?

 

Data types

This one is quite obvious. Basically you are aware that numbers are something else than text or using programming language integers are not strings. And there some activities you can do with numbers but are not possible with text. You shouldn’t use your sword to spear an enemy or your shield to axe them down .. unless you are really desperate :)

 

my_war_cry = “We will win tonight brothers !”

my_brothers_by_my side = 20

 

So here I got my_war_cry before a battle as a “text” and a total count of my_brothers_by_my side as a “number”. I can perform operations that belong to the text (or string) like printing it in upper/lower case, repeat it etc. and I can perform operations like adding, multiplying etc. with numbers (integers) but I can’t do it other way round e.g. trying to uppercase a number. It makes sense, right?

 

Conditions

Conditions are nothing mysterious. It’s the way you control the flow of the program, the way you tell your spell where to go if something happens, if some limit is reached or to trigger an action according to the situation.

 

my_clan = 30

enemy_clan = 50

 

if enemy_clan > my_clan:
    print(“They are coming in numbers”)

if enemy_clan > 100:

    print(“We die! But it’s alright, we are going to Valhalla anyway :) “)

 

This is a pretty straightforward situation. If the enemy_clan has more men than my_clan message “They are coming in numbers” displays. In case enemies are over 100 men Valhalla message would appear. 

 

Lists (and other collections)

As the name says it’s a collection or a set, leather belt with life and magic potions or a backpack where you store your items (like variables) so you stay organized and it’s easy to carry around and also easy for you to find a certain item when you need it. You did the packing so you know where the object is. If you don’t know you can systematically go through it – spot after spot and eventually you’ll find it .. so it can be used.

 

my_belt = [“life potion”, “life potion”, “poison”, “liquid flame”]

print(“I’ve drunk:”, my_belt[0])

 

result:

I ‘ve drunk: life potion

 

In this one situation you are forced to drink a flask with life potion to restore your health. So you go through the slots of your belt and because you remember the exact slot ([0] means the first position) you’ll have no trouble finding it.

 

Loops

The time for loops is the moment you realize you want to repeat an action or a sequence of steps for certain number of times or till something happens. Because you are smart and and sort of lazy .. you throw it all into a loop / cycle. All the steps within the loop are performed and only after this is done the program moves on.

 

my_life_points = 9

my_attack = 2

enemy_life_points = 7

enemy_attack = 2

number_of_rounds = 3

    

for one_round in range(number_of_rounds ):

    my_life_points = my_life_points – enemy_attack

    enemy_life_points = enemy_life_points – my_attack

 

print(“My life: “, my_life_points)

print(“Enemy life”enemy_life_points)

 

result:

My life: 3

Enemy life: 1

 

So this shows a fight between you and your enemy. You have our life points and attack abilities set (using variables) in the beginning. Then the “FOR” loops does the job – for an agreed number of times you attack each other using our skills and you bleed from these attacks as well. For loop is used for a cycle that has a specified number of repetitions or “rounds”. Now let’s take a look at the other type of a loop.

 

my_life_points = 9

my_attack = 2

enemy_life_points = 7

enemy_attack = 2

number_of_rounds = 3

 

while my_life_points > 3 and enemy_life_points > 3:

    my_life_points = my_life_points – enemy_attack

    enemy_life_points = enemy_life_points – my_attack

 

print(“My life: “, my_life_points)

print(“Enemy life”enemy_life_points)

 

result:

My life: 5

Enemy life: 3

 

The fight is the same – you attack and you bleed from attacks. It’s the battleground that changed, you are using the “WHILE” loop. Now you can attack as many times you want but only as long as your life points are greater than 3 (and because your attacks can deliver 2 damage only you will always survive with at least 1 HP. As you exchange your attack eventually one of you gets under 3 health points and when this happens the condition of the loop (>3) is no longer met and the cycle stops saving you from greater bloodshed. You may need to replenish your health after this friendly sword-fight. Let’s do it.

 

spring_of_youth = 5

my_life_points = my_life_points + spring_of_youth

enemy_life_points = enemy_life_points + spring_of_youth

 

print(“My life: “, my_life_points)

print(“Enemy life”enemy_life_points)

 

result:

My life: 10

Enemy life: 8

 

Functions

Remember the loops and being smart and lazy? You are going to move it onto another level. Let’s say you need to prepare some special spell. A spell that requires a number of different ingredients like certain runes, spoken incantations, repeating skaldic poems at midnight .. all mixed together. So every time you want to create this spell you have to undergo all the steps again trying to not to make a mistake and not to forget anything.

That’s not the way we want it to be. We like it the Viking way – swift, ready to use, efficient, deadly. So we prepare our spell and we put it into a function. And every time we want to cast this spell we just say the name of it instead of repeating all the steps.

 

def curse_of_loki():

    magic_herbs = [“Blessed Thistle”, “Dandelion”, “Sage”]

    runes_needed = [“Algiz”, “Nauthiz”, “Dagaz”]

    poem = “By the power of the mighty three I am sending Loki’s flame on you”

    for rune in runes_needed:

        print(“Rune carved onto a sigil:”, rune)

        print(poem)

    while len(magic_herbs) > 0:
         print(“I am burning this one down!”, magic_herbs[0])
         magic_herbs.remove(magic_herbs[0])
 
    if len(magic_herbs) == 0:
        print(“The Loki’s curse has been unleashed !”)
 

curse_of_loki()

 

result:

Rune carved onto a sigil: Algiz
By the power of the mighty three I am sending Loki’s flame on you
Rune carved onto a sigil: Nauthiz
By the power of the mighty three I am sending Loki’s flame on you
Rune carved onto a sigil: Dagaz
By the power of the mighty three I am sending Loki’s flame on you
I am burning this one down! Blessed Thistle
I am burning this one down! Dandelion
I am burning this one down! Sage
The Loki’s curse has been unleashed !

 

By typing curse_of_loki() you call this function and all the steps you coded into it will be executed. This is how real magic of coding works ..

 

Libraries

Whenever someone prepares an extra handy and useful set of functions / or spells it makes sense to offer them to other apprentices and adepts who are interested. It saves our time because we don’t need to prepare a spell that makes a spear run through the heart of a man if someone already “invented” it. We only need to study = so we know where to look for these functions / spells and teleport it into your grimoire / IDE and then look into it to understand what it offers and how to use it (= with the help of documentation).

 

from book_of_fire import fire_storm

fire_storm()

 

If someone writes a book_of_fire and defines fire_storm spell there, this is the way you obtain the knowledge and arcane powers.

 

from datetime import datetime

print(datetime.datetime.now())

 

If you want to have some fun with time in Python using a datetime library – this is how you get current date and time using the “now” method / function.

p.s. this post is not meant to teach programming (even though the code works in Python), it’s meant to show the principles and to try to present a general overview of the whole “system” of the art of programming 


I hope You enjoyed this Viking insight into coding :)

© 2024 Jiří Svoboda – George Freedom