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

问题描述:

直线蝙蝠我是新手编程,因此提前道歉以提出基本问题。我研究了以前的答案,但我仍然陷入困境。我正在解决以下问题:如何更改嵌套在列表中的元组的元素

问: 编写角色扮演游戏的角色创建程序。玩家应该得到一个积分,用于四个属性:力量,健康,智慧,敏捷。玩家应该能够在任何属性上消费积分,并且应该能够从属性中获得积分并将其放回池中。

我已经找到了这个解决方案,但是我并不满意,因为它使用了尚未在本书中教授的术语,并且不包含列表或词典(整个章节都是关于这个的,所以我期望最终的解决方案,需要新知识的章节练习)。 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') 
+3

你能不能请直接跟着追逐问题描述以及提供的代码? –

+1

为什么使用元组列表而不是字典? –

元组是不可改变的,因此你不能改变它们内部的值。

我建议你使用字典。

如果你要修改的元素character,您应该使用字典,或嵌套列表,或列表的元组,但你不能用一个元组列表
字典:

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点到'力量',你可以做character['Strength'] += 10,如果是字典,或者character[0][1] += 10,如果你想使用一个列表或元组