2016-06-13 12 views
-2
import random 
import time 

#universal variables 
attack = 1 
defend = 2 
heal = 3 
v = False 

player_health = 0 
player_damage = 0 
player_heal = 0 
player_block = 0 

ai_health = 0 
ai_damage = 0 
ai_heal = 0 
ai_block = 0 

#player classes in the form of dictionaries 
sniper = {} 
sniper["health"] = 50 
sniper["damage"] = random.randint(20,40) 
sniper["heal"] = random.randint(2,10) 
sniper["block"] = 5 

tank = {} 
tank["health"] = 200 
tank["damage"] = random.randint(2,8) 
tank["heal"] = random.randint(5,20) 
tank["block"] = 20 

def start(): 
    print "lets play" 
    while clas(): 
     pass 
    while game(): 
     pass 
    win() 

#get the class of the player 
def clas(): 
    while True: 
     Class = raw_input("choose a class:\nsniper = 1\ntank =2\n") #change as stats are added 
     try: 
      Class = int(Class) 
      if Class in (1,2): #change as stats are added 
        Class1 = random.randint(1,2) 

    #get the class' stats for the player 
        if Class == 1: 
         player_health = sniper["health"] 
         player_damage = sniper["damage"] 
         player_heal = sniper["heal"] 
         player_block = sniper["block"] 

        if Class == 2: 
         player_health = tank["health"] 
         player_damage = tank["damage"] 
         player_heal = tank["heal"] 
         player_block = tank["block"] 

        #get the class' stats for the ai 
        if Class1 == 1: 
         ai_health = sniper["health"] 
         ai_damage = sniper["damage"] 
         ai_heal = sniper["heal"] 
         ai_block = sniper["block"] 

        if Class1 == 2: 
         ai_health = tank["health"] 
         ai_damage = tank["damage"] 
         ai_heal = tank["heal"] 
         ai_block = tank["block"] 
        break 
     except ValueError: 
      pass 
     print "Oops! I didn't understand that. Please enter a valid number" 

上記のコードでは、clas()という関数があります。この関数には、プレーヤーとAI統計に関する8つの変数があります。これらの変数を10〜18行目の変数にコピーしたいのですが、どうすればよいか分かりません。任意の変数の使用copy.deepcopy関数の値をコピーするのが一般的でPython 2.7の関数から変数の値を返す方法は?

+1

の詳細を参照してください。 。 –

+2

http://stackoverflow.com/help/mcve –

+0

あまりにも多くのコードを追加しようとしている場合は、**あなたの要点を示すために必要最小限の例**に減らしてください。 – deceze

答えて

0

from copy import deepcopy 

z = 1 
x = {'a': {'z': 12}} 

def copy_args(z, x): 
    copy_z = deepcopy(z) 
    copy_x = deepcopy(x) 
    return copy_z, copy_x 

copy_z, copy_x = copy_args(z, x) 
print(copy_z, copy_x) 

あなたは4つのスペースでそれをインデントすることで、コードのブロックを追加することができますpython-course deepcopy articlepython copy module documentation

グッドラック

関連する問題