如何从文本文件导入内容并将其添加到变量

时间:2019-04-20 19:39:01

标签: python

我有一个垄断文件,例如我正在制作的游戏的设置文件,该设置文件具有诸如玩的人数和诸如此类的参数,我想知道如何从该文本文件导入设置并将其导入不同的变量,例如,设置文件中的玩家数量转到实际代码中的numPlayers变量,因此我可以将其用于其他游戏,也可以使用代码中的设置

这是我的代码:

    def inputs(line, typeinp=None, start=None, end=None):
        while True:
            string = input(line)
            if typeinp != None:
                try:
                    if typeinp == "string":
                        string = str(string)
                    elif typeinp == "integer":
                        string = int(string)
                    if start != None and end != None:
                        while not (string >= start and string <= end):
                            print("Please input a number between", str(start) + "-" + str(end))
                            string = int(input(line))
                    break
                except:
                    print("Plese input a", typeinp)
            else:
                break
        return string


    # Settings file, if user chooses to run setup this is all the setup questions


    def gameSettingsSetup():
        settingsCheck = open("settings.txt", "r")
        print("Lets setup")
        # Int Setup questions
        numPlayers = str(inputs('How many real players are playing: ', "integer"))
        numAIplayers = str(inputs('How many AI players will be playing?: ', "integer"))
        AILevel = str(inputs("What AI difficulty level would you like? (Easy, Normal, Hard): "))
        while True:
            if AILevel == "Easy" or AILevel == "Normal" or AILevel == "Hard":
                startingMoney = str(inputs("How much money does everyone start with? Max: 10 000 (Keep in mind "
                                           "this does not affect the property prices) ", "integer", 100, 10000))
                break

            else:
                print("Please enter a valid input (make sure to capitalize the first letter)")
                AILevel = str(inputs("What AI difficulty level would you like? (Easy, Normal, Hard): "))

        # sends over the settings into the text file as well as monoset check
        if "MonoSet1-1" in settingsCheck.read():
            with open("settings.txt", "w") as file:
                file.write("MonoSet1-1: true" + "\n")
                file.write("numPlayer: " + numPlayers + "\n")
                file.write("numAIplayer: " + numAIplayers + "\n")
                file.write("AI Level: " + AILevel + "\n")
                file.write("startingMoney: " + startingMoney + "\n")
                file.close()

        # Allows for access to the settings file and drops values into a list
        settings = []
        with open("settings.txt") as file:
            for line in file:
                line = line.split(":")
                line = line[1]
                line = line.rstrip("\n")
                line = line[1:]
                line = line.split(" ")
                try:
                    for x in range(len(line)):
                        line[x] = int(line[x])
                    line = (line[0], line[1])
                except:
                    line = line[0]
                    settings.append(line)
        file.close()
        print("Alright the setup is complete, Your game will start now....")
        time.sleep(1)

        return settingsCheck, settings

0 个答案:

没有答案