2016-06-13 79 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()的函數。在這個函數中,有8個關於球員和ai統計的變量。我想要這些變量複製到第10-18行的變量,但我不知道如何。如何從Python 2.7中的函數返回變量的值?

+1

您可以用四個空格縮進其添加的代碼塊。 –

+2

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

+0

如果它告訴你,你正在嘗試添加太多的代碼... **,然後將其降低到必要的最小示例**來說明您的觀點。 – deceze

回答

0

一般而言複製任何變量利用價值copy.deepcopy功能:

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) 

查看更多關於python-course deepcopy articlepython copy module documentation

好運

相關問題