2017-04-25 200 views
-1

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

問: 編寫角色扮演遊戲的角色創建程序。玩家應該得到一個積分,用於四個屬性:力量,健康,智慧,敏捷。玩家應該能夠在任何屬性上消費積分,並且應該能夠從屬性中獲得積分並將其放回池中。

我已經找到了這個解決方案,但是我並不滿意,因爲它使用了尚未在本書中教授的術語,並且不包含列表或詞典(整個章節都是關於這個的,所以我期望最終的解決方案,需要新知識的章節練習)。 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

爲什麼使用元組列表而不是字典? –

回答

0

元組是不可改變的,因此你不能改變它們內部的值。

我建議你使用字典。

0

如果你要修改的元素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,如果你想使用一個列表或元組