Python csv file not printing properly, for Text Based Game

66 Views Asked by At

So creating this text based game, currently just sorting out some fundamentals, when printing out the classes stats the knight class prints out totally fine but the mage class for some reason prints out two physical damage stats. This is my code.

import csv


class Character:

    health = 0
    physicalDamage = 0
    rangedDamage = 0
    magicDamage = 0
    defense = 0

    inventoryMax = 25
    inventoryList = []

    def __init__(self, name, role):
        self.name = name
        self.role = role

def ClassPicker():
    print('Select Your Class')
    with open('./tables/classes.csv', 'r') as classes:
        classes = list(csv.reader(classes))
        classStats = classes[0]
        classes.pop(0)
        while True:
            for row in classes:
                print(f'[{classes.index(row)+1}]{row[0]}')
            inspect = int(input('')) - 1
            for x in classes[inspect]:
                if classes[inspect].index(x) >= 2:
                    print(f'{classStats[classes[inspect].index(x)]}: {x}')
                else:
                    print(x)
            print('Are You Sure You Want To Select This Class')
            print('[yes] [no]')
            confirm = input('').lower()
            if confirm == 'no':
                print('Select Your Class')
            elif confirm == 'yes':
                print(classes[0])
                chosenClass = classes[0]
                break
            else:
                print('Enter A Valid Option')

        # One Time Class Stat Setting
        Character.role = chosenClass[0]
        Character.health = chosenClass[2]
        Character.physicalDamage = chosenClass[3]
        Character.rangedDamage = chosenClass[4]
        Character.magicDamage = chosenClass[5]
        Character.defense = chosenClass[6]


if __name__ == '__main__':
    Character.name = input('what is your name? ')
    print(f'Your Name Is {Character.name}?')
    while True:
        print('[yes] [no]')
        confirm = input().lower()
        if confirm == 'no':
            Character.name = input('what is your name? ')
            print(f'Your Name Is {Character.name}?')
        elif confirm == 'yes':
            break
        else:
            print('Enter A Valid Option')
    ClassPicker()

the csv file used

any help is massively appreciated and any ideas or suggestions go a long way <3

it should print Mage Highly intelligent, with an affinity towards magic. The Mage prefers to sit at range casting spells. Health: 80 Physical Damage: 5 Ranged Damage: 5 Magic Damage: 20 Defense: 2

But instead it prints like this Mage Highly intelligent, with an affinity towards magic. The Mage prefers to sit at range casting spells. Health: 80 Physical Damage: 5 Physical Damage: 5 Magic Damage: 20 Defense: 2

The knight class prints correctly and uses the exact same code so I genuinely dont understand

2

There are 2 best solutions below

0
richard On BEST ANSWER
            elif confirm == 'yes':
                print(classes[0])
                chosenClass = classes[0]
                break

this always selects the same index (Knight) as the chosen class.
also, use csv.DictReader can you can clean up all the confusing index notation.

def ClassPicker():
    print('Select Your Class')
    with open('data.csv', 'r') as classes:
        classes = list(csv.DictReader(classes))
        while True:
            for i, row in enumerate(classes, 1):
                class_name = row['Class']
                print(f'[{i}]{class_name}')
            inspect = int(input('')) - 1
            for key, val in classes[inspect].items():
                print(f'{key}: {val}')
            print('Are You Sure You Want To Select This Class')
            print('[yes] [no]')
            confirm = input('').lower()
            if confirm == 'no':
                print('Select Your Class')
            elif confirm == 'yes':
                print(classes[inspect])
                chosenClass = classes[inspect]
                break
            else:
                print('Enter A Valid Option')

        # One Time Class Stat Setting
        Character.role = chosenClass['Class']
        Character.health = chosenClass['Health']
        Character.physicalDamage = chosenClass['Physical Damage']
        Character.rangedDamage = chosenClass['Ranged Damage']
        Character.magicDamage = chosenClass['Magic Damage']
        Character.defense = chosenClass['Defense']
0
Thom On

The problem is at the line:

print(f'{classStats[classes[inspect].index(x)]}: {x}')

Notice in your data, the class Mage has the same Physical Damage with Ranged Damage (both are 5). So the program run classes[inspect].index(5) twice, which prints Physical Damage twice.

My opinion for fix is replacing this block of code:

for x in classes[inspect]:
    if classes[inspect].index(x) >= 2:
        print(f'{classStats[classes[inspect].index(x)]}: {x}')
    else:
        print(x)

with a simple dict created from the zip function:

for k, v in dict(zip(classStats, classes[inspect])).items():
    if k in ("Class", "Description"):
        print(v)
    else:
        print(f"{k}: {v}")

And it should print the thing you want.