2011-01-30 5 views
2

このテキストの練習を終えたばかりで、同じ技術(辞書、if文など)を使用してこれを効率的に実行できるかどうか疑問に思っていました。コード化するのが効率的ではありません。絶対初心者のためのPythonのプログラミング:第5章チャレンジ#2

チャレンジは次のとおりです。 ロールプレイングゲームのキャラクタークリエイタープログラムを作成します。強さ、健康、知恵、および敏捷性の4つの属性に費やすために、プレイヤーは30ポイントのプールを与えられるべきです。プレイヤーは任意のアトリビュートのプールからポイントを使うことができなければならず、アトリビュートからポイントを取得してプールに戻すこともできるはずです。

リンク:あなたは簡単に改善できるhttp://pastebin.com/PeLLz83e

+1

これは、http://codereview.stackexchange.com/ –

+0

でより適切かもしれません。 Codereviewはこれに最適です。 –

+0

user596100:ここで「ありがとう」と言うのはアップvoteです。ちょうどあなたが知っているので。 :) –

答えて

3

ことの一つだ場合、」最初のシリーズです:

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

あなたが行うことができます辞書skills_dict = {"1": "Strength", "2": "Health", ... }を使用することにより:

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

同じ第2グループのifの場合

+0

ああ、ありがとう。 – AlphaTested

0

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

変わる唯一の事は、入力が何であったか、どのようなstatに変更されますし、あなたが追加したりしているかどうか:あなたのコードが洗練思われる方法は、具体的に非常に類似した配列の多くは、そこにあるということです値を削除します。あなたは、関数のように再利用可能なコンポーネントに変換する方法を見つけたら、コードを少し見栄え良くすることができます。

+0

Cool。ありがとう。機能を学ぶのを待つことはできません。私はそれが1章か2章にあると思う。 – AlphaTested

0

いくつかのコメント:

points=int(30) 

30はすでにintです。また

points=30 

while player != "e": 

への変更は、おそらく次のようになります。

while selection != "exit" 

私は小さな関数の負荷にこのプログラムを分割し、店にキャラクターのクラスを作ると思いますスキルと残りのポイントの両方。

+0

優れています。ありがとう。 – AlphaTested

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.") 
関連する問題