Python csv文件无法正确打印,用于基于文本的游戏

cyej8jka  于 2023-09-27  发布在  Python
关注(0)|答案(3)|浏览(107)

所以创建这个基于文本的游戏,目前只是整理一些基础,当打印出职业统计的骑士职业打印出完全罚款,但 Mage 职业出于某种原因打印出两个物理伤害统计。这是我的代码。

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
任何帮助都非常感谢,任何想法或建议都有很长的路要走<3
它应该印上 Mage 高智慧,对魔法有亲和力。 Mage 喜欢坐在远处施法。生命值:80物理伤害:5远程伤害:5魔法伤害:20防御:2
但它却像这个 Mage 一样高智商,对魔法有亲和力. Mage 喜欢坐在远处施法。生命值:80物理伤害:5物理伤害:5魔法伤害:20防御:2
骑士类打印正确,使用完全相同的代码,所以我真的不明白

mdfafbf1

mdfafbf11#

elif confirm == 'yes':
                print(classes[0])
                chosenClass = classes[0]
                break

这总是选择与所选类相同的索引(Knight)。
另外,使用csv.DictReader可以清除所有令人困惑的索引符号。

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']
lrpiutwd

lrpiutwd2#

问题出在线路上:

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

注意你的数据, Mage 的物理伤害和远程伤害是一样的(都是5)。因此,程序运行classes[inspect].index(5)两次,这将打印Physical Damage两次。
我的修复意见是替换这段代码:

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

用zip函数创建一个简单的dict:

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

它应该会打印出你想要的东西。

bmp9r5qi

bmp9r5qi3#

这听起来像一个有趣的项目!在Python中,你做的一些事情有点不合常规。例如,修改Character类的类变量而不是creating an instance of that class.。我尝试重构你的代码,使其更像Pythonic
我已经将逻辑分离成更小的函数,我们现在使用csv.DictReader,并且我们正在示例化一个新的Character(),而不是修改类本身。
作为进一步的增强,您可以检查dataclass是否适合您的角色。

import csv

class Character:

    def __init__(
        self,
        name,
        character_class,
        health,
        physical_damage,
        ranged_damage,
        magic_damage,
        defense,
        inventory_max=25,
        **_kwargs,
    ):
        self.name = name
        self.character_class = character_class
        self.health = health
        self.physical_damage = physical_damage
        self.ranged_damage = ranged_damage
        self.magic_damage = (defense,)
        self.inventory_max = inventory_max
        self.inventory_list = []

    def __str__(self):
        return f'<Character: {self.name} ({self.character_class})>'

def load_classes(filename='./classes.csv'):
    # Close the file after we're done with it. We don't need to keep it
    # open while waiting for the user the make their choice.
    with open(filename, 'r') as class_file:
        return list(csv.DictReader(class_file))

def get_user_input(message, valid_choices):
    while True:
        print(message)
        user_response = input()
        if user_response in valid_choices:
            return user_response
        else:
            print(f'Invalid choice, please enter one of {valid_choices}')

def confirm(player_resp):
    if player_resp in ('y', 'yes'):
        return True
    elif player_resp in ('n', 'no'):
        return False
    else:
        raise ValueError('Invalid input')

def pick_class(list_of_class_dicts):
    while True:
        for index, class_dict in enumerate(list_of_class_dicts, start=1):
            print(f"{index}) {class_dict['character_class']}: {class_dict['description']}")

        player_resp = get_user_input(
            'Select Your Class?',
            valid_choices=[str(i) for i in range(1, len(list_of_class_dicts) + 1)],
        )
        selected = list_of_class_dicts[int(player_resp) - 1]
        for key, value in selected.items():
            print(f'{key}: {value}')
        player_confirms = confirm(
            get_user_input(
                'Are You Sure You Want To Select This Class?\n[yes] [no]',
                valid_choices=('yes', 'y', 'no', 'n'),
            )
        )
        if player_confirms:
            return selected

def build_character(available_classes):
    while True:
        player_name = input('what is your name? ')
        print(f'Your Name Is {player_name}?')
        selected_class = pick_class(available_classes)
        character = Character(player_name, **selected_class)
        print('Your character is...')
        print(character)
        player_accepts = confirm(
            get_user_input(
                'Are you happy with this choice?\n[yes] [no]',
                valid_choices=('yes', 'y', 'no', 'n'),
            )
        )
        if player_accepts:
            return character

if __name__ == '__main__':
    available_classes = load_classes()
    character = build_character(available_classes)

使用此CSV文件:

character_class,description,health,physical_damage,ranged_damage,magic_damage,defense
Mage,a wizard is never late,100,4,0,8,5
Knight,good night,150,10,2,0,10

相关问题