如何更改嵌套在列表中的元组元素

时间:2017-04-25 15:33:22

标签: python

直接蝙蝠我是编程的新手,所以提前道歉提出一个基本问题。我已经研究过以前的答案,但我仍然卡住了。我正在解决以下问题:

问: 为角色扮演游戏编写角色创建程序。应该为玩家提供一系列积分,用于四个属性:力量,健康,智慧,敏捷。玩家应该能够在任何属性上花费积分,并且还应该能够从属性中获取积分并将它们放回池中。

我找到了这个解决方案,但我不满意,因为它使用了书中尚未教授的术语,并且不包含列表或词典(整个章节都是关于此的,所以我希望结束 - 章节练习要求新知识)。 https://github.com/malmhaug/Py_AbsBegin/tree/master/Ch5E2_CreateCharacter

请给我一些帮助:这是迄今为止我所得到的。

提前致谢

#Character creator program; players have 30 points to spend across 4 attributes
#Players can also change their choices

#character attributes
character = [('Strength', 0), ('Health', 0), ('Wisdom', 0), ('Dexterity', 0)]

total = 30
int(total)
print('\nYou have ', total, 'points to spend')

choice = None
while choice != '0':
    print(
'''
Please choose an option: \n
\t0: Exit
\t1: Display CV
\t2: Denote Strength
\t3: Denote Health
\t4: Denote Wisdom
\t5: Denote Dexterity
\t6: Change an attribute

'''
)

    choice = input('Choice ')
    print()

#Exit
    if choice == '0':
        print('Goodbye')
        break
    elif choice == '1':
        print('Your Character:')
        print('Attibute\tLevel')
        for i in character:
            print(i)

    elif choice == '2': #Adds points to the Strength value whilst
                        #subtracting them from total

        character[0] = (attribute, value)
        strength = int(input('How strong is your character? '))
        if strength <= total:
            value = strength
            total -= strength
            print('You have ', total,'points remaining')
        elif strength > total:
            print('You dont have enough points')
        else:
            print('Sorry, invalid entry')

    elif choice == '3': #Adds points to the Health value whilst
                        #subtracting them from total

        attribute, value = ('Health', 0)
        health = int(input('How robust is your character? '))
        if health <= total:
            value = health
            total -= health
            print('You have ', total,'points remaining')
        elif health > total:
            print('You dont have enough points')
        else:
            print('Sorry, invalid entry')

    elif choice == '4': #Adds points to the Wisdom value whilst
                        #subtracting them from total

        attribute, value = ('Wisdom', 0)
        wisdom = int(input('How wise is your character? '))
        if wisdom <= total:
            value = wisdom
            total -= wisdom
            print('You have ', total,'points remaining')
        elif wisdom > total:
            print('You dont have enough points')
        else:
            print('Sorry, invalid entry')

    elif choice == '5': #Adds points to the Dexterity value whilst
                        #subtracting them from total

        attribute, value = ('Dexterity', 0)
        dexterity = int(input('How dextrous is your character? '))
        if dexterity <= total:
            value = dexterity
            total -= dexterity
            print('You have ', total,'points remaining')
        elif dexterity > total:
            print('You dont have enough points')
        else:
            print('Sorry, invalid entry')


    elif choice == '6': #Changes the 'value' element in the tuple, chosen by the
                        #user's input



        for skill in character: #shows the user the current character attributes
            print(skill)

        change = input('Enter an attribute to change: ')#the attriute to change
        for skill in character:#iterates over the list's tuples

            if change == skill[0]:#tests the change string against the element in the 1st position of each tuple
                total += skill[1]
                value = int(input('What do you want the new value to be? '))#sets the new value to the second element of the tuple

                if value <= total:
                        total -= new_value
                        print('You have ', total,'points remaining')
                elif new_value > total:
                    print('You dont have enough points')
                else:
                    print('Sorry, invalid entry')
input('\n\nPress enter to exit')

2 个答案:

答案 0 :(得分:0)

元组是不可变的,因此您无法更改其中的值。

我建议你使用字典。

答案 1 :(得分:0)

如果要修改character中的元素,则应使用字典,嵌套列表或列表元组,但不能使用元组列表
dict:

character = { 'Strength' : 0, 'Health' : 0, 'Wisdom' : 0, 'Dexterity' : 0 }  

列表:

character = [ ['Strength', 0], ['Health', 0], ['Wisdom', 0], ['Dexterity', 0] ] 

元组:

character = ( ['Strength', 0], ['Health', 0], ['Wisdom', 0], ['Dexterity', 0] ) 

现在,如果你想增加10点积分&#39;力量&#39; ,如果你想使用列表或元组,你可以{d}或character['Strength'] += 10 {/ 1}}