2011-01-30 25 views
2

我刚刚完成了对本文的练习,并想知道是否可以使用相同的技术(字典,if语句等等)更有效地完成此操作。代码效率低下。为绝对初学者编程的Python:第5章挑战#2

挑战如下: 编写角色扮演游戏的角色造物主程序。玩家应该获得30点的资源,用于四个属性:力量,健康,智慧和敏捷。玩家应该能够在任何属性上从池中消费点数,并且还应该能够从属性获得积分并将其放回池中。

链接:http://pastebin.com/PeLLz83e

+1

这可能更适合http://codereview.stackexchange.com/ – 2011-01-30 22:06:03

+0

是的。 Codereview是一个伟大的匹配。 – 2011-01-31 10:47:29

+0

user596100:这里说“谢谢”的方式是upvote。就这样你知道。 :) – 2011-01-31 10:48:23

回答

3

一,你可以很容易地改善的是,第一个系列“如果是:

if pts2 == "1": 
    skills["Strength"] += pts 
    points -= pts 
... 

通过使用字典skills_dict = {"1": "Strength", "2": "Health", ... },你可以这样做:

skills[skills_dict[pts2]] += pts 
points -= pts 

同对于第二组'如果的

0

那么,一个在你的代码看起来不太优雅的方式是,有很多非常相似的序列,具体是:

if rmv2 == "3": 
      if rmv < skills["Dextarity"]: 
       skills["Dextarity"] -= rmv 
       points += rmv 
      else: 
       print("No") 

如果这一变化的唯一的事情是什么输入,真是静被修改,以及是否要添加或删除一个值。如果你找到一种方法将它变成一个可重用组件,就像使用一个函数一样,你可以让你的代码看起来更好一些。

0

一些评论:

points=int(30) 

30已经是一个int。更改为

points=30 

而且

while player != "e": 

大概应该是:

while selection != "exit" 

我了拆分此程序为小功能负荷,使一类的字符来存储技能和剩余点。

0

我认为你正在寻找这样的东西。函数在第5章中没有解释,所以我没有包含任何函数。

# role playing program 
# 
# spend 30 points on strenght, health, wisdom, dexterity 
# player can spend and take points from any attribute 


# library contains attribute and points 
attributes = {"strenght": int("0"), 
      "health": "0", 
      "wisdom": "0", 
      "dexterity": "0"} 

pool = int(30) 
choice = None 
print("The Making of a Hero !!!") 
print(attributes) 
print("\nYou have", pool, "points to spend.") 

while choice != "0": 
    # list of choices 
    print(
    """ 
    Options: 

    0 - End 
    1 - Add points to an attribute 
    2 - remove points from an attribute 
    3 - Show attributes 
    """ 
    ) 
    choice = input("Choose option: ") 
    if choice == "0": 
     print("\nYour hero stats are:") 
     print(attributes) 
     print("Good-Bye.") 
    elif choice == "1": 
     print("\nADD POINTS TO AN ATTRIBUTE") 
     print("You have", pool, "points to spend.") 
     print(
     """ 
     Choose an attribute: 
      strenght 
      health 
      wisdom 
      dexterity 
     """ 
     ) 
     at_choice = input("Your choice: ") 
     if at_choice.lower() in attributes: 
      points = int(input("How many points do you want to assign: ")) 
      if points <= pool: 
       pool -= points 
       result = int(attributes[at_choice]) + points 
       attributes[at_choice] = result 
       print("\nPoints have been added.") 
      else: 
       print("\nYou do not have that many points to spend") 
     else: 
      print("\nThat attribute does not exist.") 
    elif choice == "2": 
     print("\nREMOVE POINTS FROM AN ATTRIBUTE") 
     print("You have", pool, "points to spend.") 
     print(
     """ 
     Choose an attribute: 
      strenght 
      health 
      wisdom 
      dexterity 
     """ 
     ) 
     at_choice = input("Your choice: ") 
     if at_choice.lower() in attributes: 
      points = int(input("How many points do you want to remove: ")) 
      if points <= int(attributes[at_choice]): 
       pool += points 
       result = int(attributes[at_choice]) - points 
       attributes[at_choice] = result 
       print("\nPoints have been removed.") 
      else: 
       print("\nThere are not that many points in that attribute") 
     else: 
      print("\nThat attribute does not exist.") 

    elif choice == "3": 
     print("\n", attributes) 
     print("Pool: ", pool) 
    else: 
     print(choice, "is not a valid option.") 



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