2017-03-05 32 views
0

我有以下代码:列表不复制(空)

import sys 
import os.path 
import ConfigParser 
import copy 
import time 
import colorama 
from colorama import init 
from colorama import Fore, Back, Style 
init() 

#init variables 
discoveredelements = [] 
discoveredgroups = [] 
combo = [] 
savefile = ConfigParser.ConfigParser() 
os.chdir(os.getcwd()) 

#init other stuff 
class style: 
    BOLD = '\033[1m' 
    END = '\033[0m' 

def combos(): 
    #all combos 
    combo.append(('Air', 'Air', 'Wind')) 
    combo.append(('Earth', 'Earth', 'Pressure')) 
    combo.append(('Fire', 'Fire', 'Explosion')) 
    combo.append(('Water', 'Water', 'Sea')) 
    combo.append(('Air', 'Earth', 'Dust')) 
    combo.append(('Air', 'Fire', 'Energy')) 
    combo.append(('Air', 'Water', 'Steam')) 
    combo.append(('Earth', 'Fire', 'Lava')) 
    combo.append(('Earth', 'Water', 'Swamp')) 
    combo.append(('Fire', 'Water', 'Alcohol')) 

def mainmenu(): 
    print(style.BOLD + "ALCHEMY" + style.END) 
    print(style.BOLD + "Load Game" + style.END) 
    print(style.BOLD + "New Game" + style.END) 
    print(style.BOLD + "Exit" + style.END) 
    print("Type \"load\" or \"new\" or \"exit\" ") 
    mainmenuinput = raw_input() 
    if mainmenuinput == "exit": 
     sys.exit() 
    elif mainmenuinput == "load": 
     if os.path.exists('save.ini'): 
      savefile.read('save.ini') 
      discoveredelements = savefile.get('Elements','discoveredelements') 
      print("Game Loaded") 
      rungame() 
     else: 
      print("Save file not found, check file directory or start a new game.") 
      mainmenu() 
    elif mainmenuinput == "new": 
     if os.path.exists("save.ini"): 
      print("Current save file will be overwritten. Proceed?") 
      print("Y or N") 
      overwriteinput = raw_input() 
      if overwriteinput == "Y": 
       newgame() 
       rungame() 
      elif overwriteinput == "N": 
       mainmenu() 
     else: 
      newgame() 
      rungame() 


def newgame(): 
    save = open('save.ini','w') 
    #reset data 
    savefile.add_section('Elements') 
    savefile.add_section('Groups') 
    savefile.set('Elements','discoveredelements',"") 
    savefile.set('Groups','discoveredgroups',"") 
    #adds the default elements 
    discoveredelements.append("Air") 
    discoveredelements.append("Earth") 
    discoveredelements.append("Fire") 
    discoveredelements.append("Water") 
    savefile.set('Elements','discoveredelements',discoveredelements) 
    discoveredgroups.append("Air") 
    discoveredgroups.append("Earth") 
    discoveredgroups.append("Fire") 
    discoveredgroups.append("Water") 
    savefile.set('Groups','discoveredgroups',discoveredgroups) 
    savefile.write(save) 
    save.close() 
    print("Game Loaded") 

def gameloop(): 
    #actual gameplay 
    print("Type two elements (seperately) or \"list\" or \"hint\" or \"save\" or \"exit\"") 
    gameinput = raw_input() 
    if gameinput == "list": 
     displayelements = copy.copy(discoveredelements) 
     print(','.join(map(str, displayelements))) 
     gameloop() 
    elif gameinput == "hint": 
     if (time.time() - timerstart) >= 10: 
      print('hint') 
      timerstart = time.time() 
      gameloop() 
     else: 
      print("Hint is still on cooldown") 
      gameloop() 
    elif gameinput == "save": 
     savefile.set('Elements','discoveredelements',discoveredelements) 
     savefile.set('Groups','discoveredgroups',discoveredgroups) 
     print("Game saved") 
    elif gameinput == "exit": 
     savefile.read('save.ini') 
     savelist = savefile.get('Elements','discoveredelements') 
     if len(savelist) < len(discoveredelements): 
      print("Game not saved! Do you wish to exit without saving?") 
      print("Y or N") 
      overwriteinput = raw_input() 
      if overwriteinput == "Y": 
       mainmenu() 
      else: 
       gameloop() 
    else: 
     elementA = gameinput 
     elementB = raw_input() 
     if (elementA in discoveredelements) and (elementB in discoveredelements): 
       i = 0 
       created = 0 
       while True: 
        if (combo[i][0] == elementA and combo[i][1] == elementB) or (combo[i][1] == elementA and combo[i][0] == elementB): 
         print("You created " + combo[i][2]) 
         discoveredelements.append(combo[i][2]) 
         created = 1 
         break 
        i += 1 
        if i == len(combo): 
         break 
       if created == 0: 
        print("No elements created") 
        gameloop() 
     else: 
      print("Error, using non-existent or not yet discovered elements") 
      gameloop() 

def rungame(): 
    #initializing game 
    timerstart = time.time() 
    displayelements = copy.copy(discoveredelements) 
    print(','.join(map(str, displayelements))) 
    gameloop() 

#game starts here 
print(Style.RESET_ALL) 
combos() 
mainmenu() 

当I型“负载”到控制台中,没有为displayelements输出。所以,我想看看如果列表中没有任何内容(如果copy.copy()工作或不)做印刷(displayelements),并将其印刷[] 然后我检查是否discoveredelements包含任何东西,做的: ['空气”, '地球', '火', '水'] 为什么不是copy.copy()工作?

编辑: 我宣布discoveredelements全球:

global discoveredelements 
discoveredelements = [] 

的复制仍然不能正常工作,displayelements仍然是一个空列表。

+0

顺便说一句,'gameloop'不是一个循环 - 它使用递归。游戏将在1000次移动后崩溃。 – TigerhawkT3

+0

@ TigerhawkT3哦,我不知道,你告诉我:) –

回答

0

要分配给功能的全局变量,你必须声明是全球:

discoveredelements = [] 
def func(): 
    # a new var is created in the scope of this function 
    discoveredelements = [1,2,3,4] 
func() 
print (discoveredelements) 



discoveredelements = [] 
def func(): 
    # declare as global 
    global discoveredelements 
    # assign to the global var 
    discoveredelements = [1,2,3,4] 
func() 
print (discoveredelements) 
+0

我宣布它作为一个全局的变量仍然无法打印任何东西,当我做印刷(displayelements) –

+0

你声明是全球**内*感谢* de'mainmenu'功能? – johantenbroeke

+0

哦,我没笑,感谢您的答复,现在它的作品! –