Why is this python program always giving a return of 0?

Question:

Whenever I run the program, it goes smooth without any errors. It’s just that this function always returns the value 0 and I can’t figure out why. Also, please don’t mind the names written in my language.

def calcInit():
    while True:
        vrednost = 0
        liceKazne = input("Unesite krivicno delo za lice: ")
        if liceKazne in kazne:
            vrednost += kazne[liceKazne]
        elif liceKazne == "calc.finish":
            break
        else:
            print(f"'{liceKazne}' - vrednost nije pronadjena")
    print("Lice ima: ", vrednost, "meseci.")
    print("Lice ima: ", vrednost * 1000, "novcane kazne")

There is also a whole dictionary that this function gets info from:

kazne = {
    "bezanje od policije":15,
    "kradja vozila":20,
    "kradja sluzbenih vozila":30,
    "kidnapovanje gradjanina":40,
    "kidnapovanje sluzbenih lica":60,
    "kupovina oruzija":15,
    "lazno predstavljanje":20,
    "mito i korupcija":10,
    "napad na sluzbeno lice":40,
    "napad na gradjanina":15,
    "nepostovanje suda":5,
    "hladno oruzije":10,
    "nedozvoljeno oruzije mk":25,
    "nedozvoljeno oruzije vk":40,
    "ometanje sluzbenog lica u vrsenju sluzbene duznosti":10,
    "pljacka prodavnice":20,
    "pljacka bankomata":20,
    "pljacka manje banke":40,
    "pljacka manje banke kao saucesnik":20,
    "pljacka glavne banke":200,
    "pljacka glavne banke kao saucesnik":80,
    "pljacka zlatare":120,
    "pljacka zlatare kao saucesnik":60,
    "prodaja oruzija":20,
    "posedovanje ilegalne supstance":15,
    "posedovanje prljavog novca":10,
    "ubistvo gradjanina":40,
    "pretnja smrcu":15 
}

I don’t believe you will need all of this, but please help me.

The point is that it should calculate the sum of key values in the dictionary and print them out in a whole number, and that same number * 1000.

Asked By: razyx

||

Answers:

The problem with the given code is that the variable vrednost is being reset to 0 in each iteration of the while loop. Therefore, the sum of the values from the dictionary kazne is not being accumulated properly.

To fix this, you need to move the initialization of the vrednost variable outside of the loop before the while loop starts:

def calcInit():
    vrednost = 0
    while True:
        liceKazne = input("Unesite krivicno delo za lice: ")
        if liceKazne in kazne:
            vrednost += kazne[liceKazne]
        elif liceKazne == "calc.finish":
            break
        else:
            print(f"'{liceKazne}' - vrednost nije pronadjena")
    print("Lice ima: ", vrednost, "meseci.")
    print("Lice ima: ", vrednost * 1000, "novcane kazne")

Answered By: cconsta1

You are declaring the ‘vrednost’ variable inside your while loop. So, it is getting set to 0 every time the while loop runs. Put your variable outside the while loop.
Modified code:

def calcInit():
    vrednost = 0
    while True: 
        liceKazne = input("Unesite krivicno delo za lice: ")
        if liceKazne in kazne:
            vrednost += kazne[liceKazne]
        elif liceKazne == "calc.finish":
            break
        else:
            print(f"'{liceKazne}' - vrednost nije pronadjena")
    print("Lice ima: ", vrednost, "meseci.")
    print("Lice ima: ", vrednost * 1000, "novcane kazne")
Answered By: Dinux
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.