2012-04-21 97 views
-1

我正在完成一個簡單的編程練習(我還是新的),其中我通過爲4個不同的字符屬性分配30個點來創建角色配置文件。計劃功能包括:顯示當前配置文件,創建新配置文件或更改現有配置文件。第一個和第二個功能工作正常,但最後一個問題:該程序是爲了解開嵌套列表項目(屬性+分配的分數),要求新的分數,採取舊的和新的差異,並改變數字相應的池中可用點數。最後,在位置0處向列表添加一個新條目(屬性+新分配的分數),然後刪除位置1處的條目,該條目應該是該屬性的舊條目。遍歷列表並完成。但是,一旦執行代碼,您將看到它不起作用。請參閱下面的完整代碼:Python嵌套列表 - 更改和替換單個項目

options = ["Strength", "Health", "Wisdom", "Dexterity"] 
profile = [] 
points = 30 

choice = None 
while choice != "0": 

    print(
     """ 
    CHARACTER CREATOR PROGRAM 

    0 - Exit 
    1 - See current profile 
    2 - Build new profile 
    3 - Amend existing profile 

    """ 
     ) 

    choice = input("Please choose an option: ") 
    print() 

    if choice == "0": 
     print("Good bye.") 
    elif choice == "1": 
     for item in profile: 
      print(item) 
     input("\nPress the enter key to continue.") 
    elif choice == "2": 
     print("You can now equip your character with attributes for your adventures.") 
     print("You have",points,"points to spent.") 
     print("Now configure your character: \n") 
     #Run the point allocation loop 
     for item in options: 
      point_aloc = int(input("Please enter points for " + str(item) + ":")) 
      if point_aloc <= points: 
       entry = item, point_aloc 
       profile.append(entry) 
       points = points - point_aloc 
       print("\nYour current choice looks like this: ") 
       print(profile) 
       input("\nPress the enter key to continue.") 
      else: 
       print("Sorry, you can only allocate", points," more points!") 
       print("\nYour current choice looks like this: ") 
       print(profile) 
       input("\nPress the enter key to continue.") 
     print("\nWell done, you have configured your character as follows: ") 
     for item in profile: 
      print(item) 
     input("Press the enter key to continue.") 
    elif choice == "3": 
     print("This is your current character profile:\n") 
     for item in profile: 
      print(item) 
     print("\nYou can change the point allocation for each attribute.") 
     for item in profile: 
      point_new = int(input("Please enter new points for " + str(item) + ":")) 
      attribute, points_aloc = item 
      diff = points_aloc - point_new 
      if diff >0: 
       points += diff 
       print("Your point allocation has changed by", -diff,"points.") 
       print(diff,"points have just been added to the pool.") 
       print("The pool now contains", points,"points.") 
       entry = item, point_new 
       profile.insert(0, entry) 
       del profile[1] 
       input("Press the enter key to continue.\n") 
      elif diff <0 and points - diff >=0: 
       points += diff 
       print("Your point allocation has changed by", -diff,"points.") 
       print(-diff,"points have just been taken from the pool.") 
       print("The pool now contains", points,"points.") 
       entry = item, point_new 
       profile.insert(0, entry) 
       del profile[1] 
       input("Press the enter key to continue.\n") 
      elif diff <0 and points - diff <=0: 
       print("Sorry, but you don't have enough points in the pool!") 
       input("Press the enter key to continue.\n") 
    else: 
     print("Sorry, but this is not a valid choice!") 
     input("Press the enter key to continue.\n") 

input("\n\nPress the enter key to exit.") 

注意:您需要先創建配置文件以運行更改。

在此先感謝您的幫助!

+5

歡迎來到Stack Overflow。你能否澄清你的問題?它應該包含一個[簡短,獨立,正確的例子](http://sscce.org/);在嘗試時明確描述問題所在(包括錯誤消息!)和[您嘗試過的內容](http://mattgemmell.com/2008/12/08/what-have-you-tried/)的描述解決問題。 – Ben 2012-04-21 15:13:14

+4

另外,它應該包含預期的輸入和輸出。 – 2012-04-21 15:18:26

回答

0

正如您對問題的評論所指出的,您並未以最佳方式提出您的問題。但是,我發現它有什麼問題。我可以告訴你如何解決你當前的代碼,但事實是,解決這個問題的方法是完全重寫它。當您這樣做時,您應該採用以下策略:

  1. 將代碼分解爲多個部分。在這種情況下,我建議你創建幾個不同的功能。一個可以被稱爲main_loop,並將包含循環菜單的邏輯。它不包含更新或顯示配置文件的任何代碼。相反,它會調用其他函數,display_profile,build_profileamend_profile。這些函數將接受變量,如options,profilepoints,並返回值如optionspoints。這將極大地簡化您的代碼,並使測試和調試變得更加容易。這是一個什麼樣main_loop可能看起來像一個例子:

    def main_loop(): 
        options = ["Strength", "Health", "Wisdom", "Dexterity"] 
        profile = [] 
        points = 30 
    
        choice = None 
        while choice != "0": 
         print(menu_string) #define menu_string elsewhere 
         choice = input("Please choose an option: ") 
         print() 
    
         if choice == "0": 
          print("Good bye.") 
         elif choice == "1": 
          display_profile(profile) 
         elif choice == "2": 
          profile, points = build_profile(options, profile, points) 
         elif choice == "3": 
          profile, points = amend_profile(profile, points) 
         else: 
          print("Sorry, but this is not a valid choice!") 
          input("Press the enter key to continue.\n") 
    
        input("\n\nPress the enter key to exit.") 
    

    看到多少更好這是什麼?現在您只需定義其他功能即可。像...

    def build_profile(options, profile, points): 
         # insert logic here 
         return profile, points 
    

    這種方法的另一個好處是,現在你可以單獨測試這些功能,而無需運行整個程序。

  2. 使用正確的成語進行列表修改。在迭代時修改列表需要格外小心,並且在某些情況下(例如,通過刪除或添加已經迭代的項目來更改列表的長度),它根本無法工作。有些方法可以對profile做些什麼,但對於初學程序員,我會推薦一些更簡單的方法:只需創建一個新列表!然後返回該列表。因此,在您amend_profile功能,做這樣的事情:

    def amend_profile(profile, points): 
         # other code ... 
         new_profile = [] 
         for item in profile: 
          attribute, points_aloc = item 
          # other code ... 
          new_proflie.append(entry) 
         # other code ... 
    
         return new_profile, points 
    

    還請注意,這是你的主要缺陷之一;您將創建一個entry,其中包含(item, point_new)而不是(attribute, point_new),因此您的新元組中有一個item元組,而不是如預期的那樣只有一個attribute字符串。

+0

非常感謝您的幫助!並感謝所有其他意見;這是我在這裏的第一篇文章。未來將在船上接受建議:-) – Matthias 2012-04-21 16:55:30