列表不复制(空)

时间:2017-03-05 23:52:43

标签: python python-2.7

我有以下代码:

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()

当我在控制台中输入“load”时,没有输出任何显示元素。所以我试着通过print(displayelements)来查看列表是否包含任何内容(如果copy.copy()是否有效)并打印[] 然后我检查了discoveredelements是否包含任何东西,它确实: ['空气','地球','火','水'] 为什么copy.copy()不起作用?

编辑: 我宣布discoveredelements为全球:

global discoveredelements
discoveredelements = []

复制仍无效,显示元素仍为空列表。

1 个答案:

答案 0 :(得分:0)

要分配给函数中的全局变量,必须声明为全局:

ElementName