我剛剛完成了對本文的練習,並想知道是否可以使用相同的技術(字典,if語句等等)更有效地完成此操作。代碼效率低下。爲絕對初學者編程的Python:第5章挑戰#2
挑戰如下: 編寫角色扮演遊戲的角色造物主程序。玩家應該獲得30點的資源,用於四個屬性:力量,健康,智慧和敏捷。玩家應該能夠在任何屬性上從池中消費點數,並且還應該能夠從屬性獲得積分並將其放回池中。
鏈接:http://pastebin.com/PeLLz83e
我剛剛完成了對本文的練習,並想知道是否可以使用相同的技術(字典,if語句等等)更有效地完成此操作。代碼效率低下。爲絕對初學者編程的Python:第5章挑戰#2
挑戰如下: 編寫角色扮演遊戲的角色造物主程序。玩家應該獲得30點的資源,用於四個屬性:力量,健康,智慧和敏捷。玩家應該能夠在任何屬性上從池中消費點數,並且還應該能夠從屬性獲得積分並將其放回池中。
鏈接:http://pastebin.com/PeLLz83e
一,你可以很容易地改善的是,第一個系列「如果是:
if pts2 == "1":
skills["Strength"] += pts
points -= pts
...
通過使用字典skills_dict = {"1": "Strength", "2": "Health", ... }
,你可以這樣做:
skills[skills_dict[pts2]] += pts
points -= pts
同對於第二組'如果的
那麼,一個在你的代碼看起來不太優雅的方式是,有很多非常相似的序列,具體是:
if rmv2 == "3":
if rmv < skills["Dextarity"]:
skills["Dextarity"] -= rmv
points += rmv
else:
print("No")
如果這一變化的唯一的事情是什麼輸入,真是靜被修改,以及是否要添加或刪除一個值。如果你找到一種方法將它變成一個可重用組件,就像使用一個函數一樣,你可以讓你的代碼看起來更好一些。
一些評論:
points=int(30)
30已經是一個int。更改爲
points=30
而且
while player != "e":
大概應該是:
while selection != "exit"
我了拆分此程序爲小功能負荷,使一類的字符來存儲技能和剩餘點。
我認爲你正在尋找這樣的東西。函數在第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.")
這可能更適合http://codereview.stackexchange.com/ – 2011-01-30 22:06:03
是的。 Codereview是一個偉大的匹配。 – 2011-01-31 10:47:29
user596100:這裏說「謝謝」的方式是upvote。就這樣你知道。 :) – 2011-01-31 10:48:23