Home > Software design >  Printing Dictionary after appending only shows latest dictionary entry instead of the full dictionar
Printing Dictionary after appending only shows latest dictionary entry instead of the full dictionar

Time:08-20

I'm trying to create a blank dictionary that we add user inputs to (Character Name, Age, HP). Later on I will use those inputs to create new classes instances.

Here's the result as well as a note about what I was expecting to see:

PIC of The result I'm getting

I believe the problem lies in this part of the code but I'm not sure what's wrong with it.

newCharacterList = {"Name": [], "Age" : [], "HP" : []}


newCharacterList["Name"].append(newCharacterName)
newCharacterList["Age"].append(newCharacterAge2)
newCharacterList["HP"].append(newCharacterHP)

print(newCharacterList)

Full code:

import random
# Parent class of Character (Name, Age, HP) - keep it simple - no atk and defense. just keep losing HP based on a random number between 1 and 50 and once it reaches 0 it says "Whoops u died lol." but it prints out on a different row each time, dmg taken and HP remaining (maybe the random number lasts 5 x and each character may survive or die each round depending on how much hp they have.)


class Character:
    def __init__(self, name, age: int, HP):
        self.name = name
        self.age = age
        self.HP = HP

    def get_name(self):
        return self.name

    def get_age(self):
        return self.name

    def get_HP(self):
        return self.HP

    def set_HP(self):
        subtraction = int(random.randint(1,50))
        print("HP Loss: -",subtraction)
        self.HP = self.HP - subtraction

    def ShowAttributes(self):
        print("Name: ", self.name)
        print("Age: ", self.age)
        print("HP: ", self.HP)

Melo = Character("Melo", 15, 100)
Melo.ShowAttributes()
print("\n")

Halo = Character("Halo", 20, 500)
Halo.ShowAttributes()
print("\n")

Sky = Character("Sky", 18, 300)
Sky.ShowAttributes()
print("\n")


# add option to ADD a character which will take the Name, Age, and Randomly assign an HP.
# it will then note "HP Assigned. Name has _ HP."
# Press X to Start the game, A to Add a character, or any other key to Exit.

while True:

    # print(ShowAttributes())
    print("Press X to Start, A to Add a character, or any other Key to Exit\n");
    button = input("Your Input: ")

        #   #round 1 (- random number of HP once)
        #   #round 2 (- random number of HP TWICE)
        #   #round 3 (- random number of HP Three times)
        #   #round 4 (- random number of HP 4 times)
        #   #round 5 (- random number of HP 5 times)
        #   # when ALL character's HP = 0 OR all 5 rounds are finished, whichever comes first, print the order of winners (ie who survived the longest), then break and exit.


    if button == "x":
        # While LOOP (until all rounds are done OR all characters run out of HP) {
        print("\n")
        # }
        while Melo.get_HP() > 0:
                # print("its working"); #< Test Completed!! - it works!

        # While LOOP (until all rounds are done OR all characters run out of HP)
                print("Current HP: ", Melo.get_HP())
                            
                Melo.set_HP()

        if Melo.get_HP() <=0:
            print(f"Whoops, {Melo.name} has No More HP!")
            break

    elif button == "a":

        class add_Character(Character):
            def __init__(self):
                super().__init__()
                self.HP = HP
        print("In Development - for Phase 2");

        print("New Character Name: ")
        newCharacterName = input()

        print("New Character Age: ")
        newCharacterAge = input()
        newCharacterAge2 = int(newCharacterAge)

        if newCharacterAge2 < 10:
            newCharacterHP = (random.randint(1,50))

        elif newCharacterAge2 > 10 and newCharacterAge2 < 25:
            newCharacterHP = (random.randint(50, 300))

        elif newCharacterAge2 < 25:
            newCharacterHP = (random.randint(300, 1000))
        else:
            newCharacterHP = (random.randint(1,1000))

        print("Your New Character's Autogenerated HP is: ", newCharacterHP)
        

        newCharacterList = {"Name": [], "Age" : [], "HP" : []}


        newCharacterList["Name"].append(newCharacterName)
        newCharacterList["Age"].append(newCharacterAge2)
        newCharacterList["HP"].append(newCharacterHP)

        print(newCharacterList)


    else:
        break
else:
    exit

CodePudding user response:

Move newCharacterList = {"Name": [], "Age" : [], "HP" : []} before your while statement.

A few comments to improve your code:

  • if Melo.get_HP() <=0: should be removed (useless considering the while above).
  • random.randint(1,50) is already an int, you don't have to write int(random.randint(1,50))
  • Your get_age method actually returns the name. By the way, is there a particular reason to use these getters?
  • Your condition elif newCharacterAge2 < 25: is possible only when the age is 10, so it is a weird way to test newCharacterAge2 == 10.
  • Instead of ShowAttributes, use __str__ as below (then, just try to print(Melo) to see the magic happen):
def __str__(self):
    return f"Name: {self.name}\nAge: {self.age}\nHP: {self.HP}"
  • Related