Nadpisanie wartości kluczy w słowniku

0

Cześć,
mam problem z nadpisywaniem wartości w słowniku. Mianowicie stworzyłem słownik z potworami każdemu przypisując odpowiednią ilość życia. W momencie kiedy potwór zostanie zabity, wartość jego życia jest nadpisywana w słowniku i po ponownym spotkaniu go, wartość ta jest na minusie. Będę wdzięczny za pomoc.

playerDamage = 4

monsterLife = {
    Monster.Rat: 5,
    Monster.Bat: 10,
    Monster.GiantSpider: 15,
    Monster.Wolf: 30
}

def weapon_hit(chance):
    if_hit = random.uniform(1, 100)
    if if_hit < chance:
        monsterLife[drawnMonster] = monsterLife[drawnMonster] - playerDamage
        print("You hit a monster and you dealt 4 damage. It has", monsterLife[drawnMonster], " life")
    else:
        print("You missed")
        

def monster_draw():
    global goldAcquire
    global Gold
    global gameLength
    print("Oh no, you have find", drawnMonster.value, "which has", monsterLife[drawnMonster],
          "life .If you will defeat him, you will find great treasure.")
    eventAnswer = restrict_input('fight', 'run')
    if eventAnswer == "fight":
        while monsterLife[drawnMonster] > 0:
            weapon_hit(70)
            if monsterLife[drawnMonster] > 0:
                monster_hit()
                if playerLife <= 0:
                    print("You died, your body will lay in chambers forever")
                    sys.exit(0)
        drawnPremiumChest = random.choices(PremiumChestList, PremiumChestProbability)[0]
        goldAcquire = find_aprox_value(colorValue[drawnPremiumChest])
        print("Congratulations, you have defeat a monster, and you found", drawnPremiumChest.value,
              ", inside was", goldAcquire, " gold")
        Gold = Gold + goldAcquire
        gameLength = gameLength - 1
    elif eventAnswer == "run":
        gameLength = gameLength - 1
        print("you have successfully run")

0

To się nie skompiluje, nie podałeś całego kodu; a co do problemu, jeśli Ci nadpisuje globalny słwonik, to stwórz kopie, i, podczas walki pracuj na niej.

0

Ok wrzucam cały kod. CO do skopiowania listy, to musiałbym dla każdego zdarzenia robić nową kopię listy, a takich zdarzeń może być 10. Chyba, że tworzenie nowej kopii można zrobić automatycznie, jeśli tak to nie wiem jak.

import random
import sys

from enum import Enum

playerDamage = 4
playerLife = 100
gameLength = 10
Gold = 0


def restrict_input(*acceptable):
    answer = ""
    while answer not in acceptable:
        answer = input(f"What is your choice {acceptable}? ")
    return answer


def chest_draw():
    global Gold
    global gameLength
    drawnChest = random.choices(chestList, chestProbability)[0]
    goldAcquire = find_aprox_value(colorValue[drawnChest])
    Gold = Gold + goldAcquire
    gameLength = gameLength - 1
    print("You have find ", drawnChest.value, "inside was", goldAcquire, "gold")


def find_aprox_value(value):
    lowestValue = 0.9 * value
    highestValue = 1.1 * value
    return random.randint(lowestValue, highestValue)


def weapon_hit(chance):
    if_hit = random.uniform(1, 100)
    if if_hit < chance:
        monsterLife[drawnMonster] = monsterLife[drawnMonster] - playerDamage
        print("You hit a monster and you dealt 4 damage. It has", monsterLife[drawnMonster], " life")
    else:
        print("You missed")


def monster_draw():
    global playerLife
    global goldAcquire
    global Gold
    global gameLength
    print("Oh no, you have find", drawnMonster.value, "which has", monsterLife[drawnMonster],
          "life .If you will defeat him, you will find great treasure.")
    eventAnswer = restrict_input('fight', 'run')
    if eventAnswer == "fight":
        while monsterLife[drawnMonster] > 0:
            weapon_hit(70)
            if monsterLife[drawnMonster] > 0:
                monster_hit()
                if playerLife <= 0:
                    print("You died, your body will lay in chambers forever")
                    sys.exit(0)
        drawnPremiumChest = random.choices(PremiumChestList, PremiumChestProbability)[0]
        goldAcquire = find_aprox_value(colorValue[drawnPremiumChest])
        print("Congratulations, you have defeat a monster, and you found", drawnPremiumChest.value,
              ", inside was", goldAcquire, " gold")
        Gold = Gold + goldAcquire
        gameLength = gameLength - 1
    elif eventAnswer == "run":
        run()
        gameLength = gameLength - 1
        print("you have successfully run")



def monster_hit():
    global playerLife
    monsterHit = monsterDamage[drawnMonster]
    playerLife = playerLife - monsterHit
    print("Monster did", monsterDamage[drawnMonster], "damage")
    print("You have ", playerLife, "life")

def run():
    global playerLife
    chance_to_run = 10
    chance_to_fail = random.randint(1,100)
    if chance_to_run > chance_to_fail:
        print("you managed to escape")
    else:
        print(" Oh no, while trying to escape, the monster attacked you from behind and inflicted critical damage on "
              "you. Get ready to fight")



Event = Enum('Event', ['Chest', 'Monster'])
Chest = Enum('Chest', {'greenChest': 'zieloną skrzynię',
                       'blueChest': 'niebieską skrzynię',
                       'violetChest': 'fioletową skrzynię',
                       'orangeChest': 'pomarańczową skrzynię'
                       })
Monster = Enum('Monster', {'Rat': 'Szczura',
                           'Bat': 'Nietoperza',
                           'GiantSpider': 'Ogromnego Pająka',
                           'Wolf': 'Wilka',
                           })

Color = Enum('Color', ['greenChest', 'blueChest', 'violetChest', 'orangeChest'])
MonsterKind = Enum('MonsterKind', ['Rat', 'Bat', 'GiantSpider', 'Wolf'])

eventDictionary = {

    Event.Chest: 0.4,
    Event.Monster: 0.6
}

eventList = list(eventDictionary.keys())
eventProbability = list(eventDictionary.values())

chestDictionary = {
    Chest.greenChest: 0.5,
    Chest.blueChest: 0.3,
    Chest.violetChest: 0.15,
    Chest.orangeChest: 0.05
}
PremiumChestDictionary = {
    Chest.blueChest: 0.5,
    Chest.violetChest: 0.35,
    Chest.orangeChest: 0.15
}

MonsterDictionary = {
    Monster.Rat: 0.5,
    Monster.Bat: 0.3,
    Monster.GiantSpider: 0.15,
    Monster.Wolf: 0.05
}

chestList = list(chestDictionary.keys())
chestProbability = list(chestDictionary.values())

MonsterList = list(MonsterDictionary.keys())
MonsterProbability = list(MonsterDictionary.values())

PremiumChestList = list(PremiumChestDictionary.keys())
PremiumChestProbability = list(PremiumChestDictionary.values())

colorValue = {
    Chest.greenChest: 1000,
    Chest.blueChest: 4000,
    Chest.violetChest: 9000,
    Chest.orangeChest: 16000
}
monsterLife = {
    Monster.Rat: 5,
    Monster.Bat: 10,
    Monster.GiantSpider: 15,
    Monster.Wolf: 30
}
monsterDamage = {
    Monster.Rat: 3,
    Monster.Bat: 5,
    Monster.GiantSpider: 8,
    Monster.Wolf: 12
}

while gameLength > 0:

    gameAnswer = input("Do you want to move forward? \n")

    if gameAnswer == "yes":
        print("Great, lets see what is inside")
        drawnEvent = random.choices(eventList, eventProbability)[0]

        if drawnEvent == Event.Chest:
            chest_draw()

        elif drawnEvent == Event.Monster:
            drawnMonster = random.choices(MonsterList, MonsterProbability)[0]
            monster_draw()
    else:
        print("Your only options is move forward")

print("Congratulations you have acquired", Gold, "gold")


4

Tak to napisałeś, że modyfikujesz globalne zmienne, a dziwisz sie, że są zmodyfikowane.:) Albo, nawet inaczej, wywal te globalne słowniki, i twórz je sobie w funkcji na początku pętli.

0

Dzięki na to nie wpadłem, dla swojego usprawiedliwienia dopiero uczę się programowania :) .

1

Czemu robisz milion słowników zamiast jednego:

chestDictionary = {
    Chest.greenChest: { 'probability':0.5,'color':1000,'premium':0 },
    Chest.blueChest: { 'probability':0.3,'color':4000,'premium':0.5 },
    Chest.violetChest: { 'probability':0.15,'color':9000,'premium':0.35 },
    Chest.orangeChest: { 'probability':0.05,'color':16000,'premium':0.15 },
}

Albo jeszcze lepiej zrobić klasę.

Waga nie musi się sumować do 1 więc nieco bardziej czytelne:

chestDictionary = {
    Chest.greenChest: { 'probability':9,'color':1000,'premium':0 },
    Chest.blueChest: { 'probability':6,'color':4000,'premium':1 },
    Chest.violetChest: { 'probability':3,'color':9000,'premium':7 },
    Chest.orangeChest: { 'probability':1,'color':16000,'premium':3 },
}

1 użytkowników online, w tym zalogowanych: 0, gości: 1