Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Script Generator
#1
I am trying to fix an old script but am new to python.
The script all works except for when it needs to write the player's name into the script.

The Problem: It only broadcast the last name instead of the full name of the player

Info about the situation and script:
-This script generator creates scripts to run on a game server to control who can play and who can spectate.
-The script uses two external files(lineup.txt & signups.txt) to determine who can play in the next round.
-The game has a UID(unique identifier) for each player and this is used in commands to control whether they play or not.
-The lineup file is grabbed from ingame to determine who is in the server. This file also shows what server slot the player is in, their uid, what state they are in(playing or spectating), their chosen number, which dirt bike size, brand and year and finally their name ingame.
-The signups file is grabbed from a website that manages signups and qualifying times(for the racing game).
-The script in a nutshell compares the uids from each file and discards any that aren't on both list(so any players who didn't show up or sign up to the event are not included)
[Image: MYrhjrM]


This is the main script:
def heatsplitter():
    #MXS Heat Splitter v4 by Sammy Holt

    #This script will take information from a lineup file and generate heat scripts to be run by mxserver
    #This version will use a signup file to parse the lineup file and only include the UIDS which are signed up
    #Make sure all output files are completely empty
    #Also, paste lineup file data into "lineuptest.txt"
    #DO NOT RENAME FILES, the script will fail
    #If you must change data, simply cut and paste into the designated file

    #Planned Features:
    #-Add a user defined amount of heats(In Progress)
    #-Easier file handling(Timeframe Unknown)

    #This section takes input from the user for initial file generation

    print "MX Simulator Heat Splitter w/Signup Parsing"
    print "Created by Sammy Holt"
    print "\n"
    print "\n"
    lineup = raw_input('Drag and Drop your lineup file: ')
    signups = raw_input('Drag and Drop your signups file: ')
    h1 = raw_input('Enter a name for your heat 1 script: ')
    h2 = raw_input('Enter a name for your heat 2 script: ')
    racename = raw_input('Enter a race name for heat 1: ')
    racename2 = raw_input('Enter a race name for heat 2: ')
    uidname = raw_input('Enter a name for uid export: ')
    titlecolor = raw_input('Which color would you like the title to be?(Red,Green,Aqua,Blue,White,Black,Yellow,or Purple): ')
    namecolor = raw_input('Which color would you like the rider names to be?(Red,Green,Aqua,Blue,White,Black,Yellow,or Purple): ')
    tracknum = raw_input('What is the number of the track the race will be held on?: ')
    numLaps = raw_input('How many laps will these heats be?: ')
    numTransfer = raw_input('How many people transfer from each heat?: ')
    numWarmup = raw_input('Track number of warmup lap: ')


    #Color Switcher for Title:
    #This block turns the user color string into a useable term
    if (titlecolor=='Red') or (titlecolor=='red'):
        tc='31'
    elif titlecolor=='Green' or (titlecolor=='green'):
        tc='32'
    elif titlecolor=='Aqua' or (titlecolor=='aqua'):
        tc='36'
    elif titlecolor=='Blue'  or (titlecolor=='blue'):
        tc='34'
    elif titlecolor=='White' or (titlecolor=='white'):
        tc='37'
    elif titlecolor=='Black' or (titlecolor=='black'):
        tc='30'
    elif titlecolor=='Yellow' or (titlecolor=='yellow'):
        tc='33'
    elif titlecolor=='Purple' or (titlecolor=='purple'):
        tc='35'
    else:
        print "Not a valid color"

    #Color Switcher for Names:
    #This block turns the user color string into a useable term
    if namecolor=='Red' or (namecolor=='red'):
        nc='31'
    elif namecolor=='Green' or (namecolor=='green'):
        nc='32'
    elif namecolor=='Aqua' or (namecolor=='aqua'):
        nc='36'
    elif namecolor=='Blue' or (namecolor=='blue'):
        nc='34'
    elif namecolor=='White' or (namecolor=='white'):
        nc='37'
    elif namecolor=='Black' or (namecolor=='black'):
        nc='30'
    elif namecolor=='Yellow' or (namecolor=='yellow'):
        nc='33'
    elif namecolor=='Purple' or (namecolor=='purple'):
        nc='35'
    else:
        print "Not a valid color"

    #Create a variable for half the amount of riders
    #halfriders = (amtRiders/2)

    #Generate the titles and some other settings for the two scripts                
    heat1script = open("C:\\Scripts\\"+h1+".mxs","a")
    heat2script = open("C:\\Scripts\\"+h2+".mxs","a")
    heat1warmup = open("C:\\Scripts\\"+h1+"warmup"+".mxs","a")
    heat2warmup = open("C:\\Scripts\\"+h2+"warmup"+".mxs","a")
    heat1nr = open("C:\\Scripts\\"+h1+"nr"+".mxs","a")
    heat2nr = open("C:\\Scripts\\"+h2+"nr"+".mxs","a")
    heat1nrwarmup = open("C:\\Scripts\\"+h1+"nrwarmup"+".mxs","a")
    heat2nrwarmup = open("C:\\Scripts\\"+h2+"nrwarmup"+".mxs","a")

    heat1script.write("#mxserverscript"+"\n")
    heat1script.write("broadcast ["+tc+"m ---------------------------------------------------------------"+"\n")
    heat1script.write("broadcast ["+tc+"m "+racename+" - "+numLaps+" Laps - "+"Top "+numTransfer+" Transfer"+"\n")
    heat1script.write("broadcast ["+tc+"m ---------------------------------------------------------------"+"\n")
    heat1script.write("allignore all"+'\n')
    heat1script.write("settrack "+tracknum+'\n')
    heat1script.write("forcespec all"+'\n')
    heat1script.close()

    heat2script.write("#mxserverscript"+"\n")
    heat2script.write("broadcast ["+tc+"m ---------------------------------------------------------------"+"\n")
    heat2script.write("broadcast ["+tc+"m "+racename2+" - "+numLaps+" Laps - "+"Top "+numTransfer+" Transfer"+"\n")
    heat2script.write("broadcast ["+tc+"m ---------------------------------------------------------------"+"\n")
    heat2script.write("allignore all"+'\n')
    heat2script.write("settrack "+tracknum+'\n')
    heat2script.write("forcespec all"+'\n')
    heat2script.close()

    #Warmup Files
    heat1warmup.write("#mxserverscript"+"\n")
    heat1warmup.write("broadcast ["+tc+"m ---------------------------------------------------------------"+"\n")
    heat1warmup.write("broadcast ["+tc+"m "+racename+" - "+"Lineup"+"\n")
    heat1warmup.write("broadcast ["+tc+"m ---------------------------------------------------------------"+"\n")
    heat1warmup.write("forcespec all"+"\n")
    heat1warmup.write("settrack "+numWarmup+"\n")
    heat1warmup.close()
    heat2warmup.write("#mxserverscript"+"\n")
    heat2warmup.write("broadcast ["+tc+"m ---------------------------------------------------------------"+"\n")
    heat2warmup.write("broadcast ["+tc+"m "+racename2+" - "+"Lineup"+"\n")
    heat2warmup.write("broadcast ["+tc+"m ---------------------------------------------------------------"+"\n")
    heat2warmup.write("forcespec all"+"\n")
    heat2warmup.write("settrack "+numWarmup+"\n")
    heat2warmup.close()

    #Next Race Script
    heat1nr.write("#mxserverscript"+"\n")
    heat1nr.write("broadcast  Next Race is: "+racename+"\n")
    heat1nr.close()
    heat1nrwarmup.write("#mxserverscript"+"\n")
    heat1nrwarmup.write("broadcast  Next Race is: "+racename+" Warmup Lap"+"\n")
    heat1nrwarmup.close()
    #Next Race Script2
    heat2nr.write("#mxserverscript"+"\n")
    heat2nr.write("broadcast  Next Race is: "+racename2+"\n")
    heat2nr.close()
    heat2nrwarmup.write("#mxserverscript"+"\n")
    heat2nrwarmup.write("broadcast  Next Race is: "+racename2+" Warmup Lap"+"\n")
    heat2nrwarmup.close()

    #Add automatic calls to Broadcast Next Race
    heat1script = open("C:\\Scripts\\"+h1+".mxs","a")
    heat2script = open("C:\\Scripts\\"+h2+".mxs","a")
    heat1warmup = open("C:\\Scripts\\"+h1+"warmup"+".mxs","a")
    heat2warmup = open("C:\\Scripts\\"+h2+"warmup"+".mxs","a")

    #heat1script.write("at +2 broadcast  Next Race is: "+racename+"\n")
    heat1script.close()
    #heat2script.write("at +2 broadcast  Next Race is: "+racename2+"\n")
    heat2script.close()

    #heat1warmup.write("at +2 broadcast  Next Race is: "+racename+" Warmup Lap"+"\n")
    heat1warmup.close()
    #heat2warmup.write("at +2 broadcast  Next Race is: "+racename2+" Warmup Lap"+"\n")
    heat2warmup.close()

    #Stuff used to split heats evenly
    with open(signups) as f:
            lines = f.read().splitlines()
            
    num_lines = sum(1 for line in open(lineup))

    p=0
    with open(lineup,"r") as f:
        data = f.readlines()

        for line in data:
            words=line.split()
            userid=words[1]

            if userid in lines:
                p+=1
    nRacers = (p/2)

    #This section will deal with the actual lineup file
    x=0
    y=0
    z=0
    with open(lineup,"r") as f:
        data = f.readlines()

        for line in data:
            #Open files for output
            heat1=open("C:\\Scripts\\"+h1+".mxs","a")
            heat2=open("C:\\Scripts\\"+h2+".mxs","a")
            heat1warmup=open("c:\\Scripts\\"+h1+"warmup"+".mxs","a")
            heat2warmup=open("c:\\Scripts\\"+h2+"warmup"+".mxs","a")
            uidfile=open("c:\\Scripts\\"+uidname+".txt","a")
            #Take lineup file into a list
            words=line.split()
            
            #Make a slot variable
            slot=words[0]
            
            #Set a variable equal to names and uids of each slot
            #Also cleans up the underscores from the lineup file and replaces them with a whitespace
            userid=words[1]

            
            #The following will generate the heats (AKA. any bugs are probably here)
            if userid in lines:
                if x<nRacers:
                    heat1.write("broadcast ["+nc+"m " + username + "\n")
                    heat1.write("forceplay " + userid + "\n")
                    heat1.write("broadcastto " + userid + "  You are in: "+racename + "\n")
                    heat1.close()
                    heat1warmup.write("broadcast ["+nc+"m " + username + "\n")
                    heat1warmup.write("forceplay " + userid + "\n")
                    heat1warmup.write("broadcastto " + userid + "  You are in: "+racename + "\n")
                    heat1warmup.close()
                    uidfile.write(userid+"\n")
                    uidfile.close()
                    z+=1
                else:
                    heat2.write("broadcast ["+nc+"m " + username + "\n")
                    heat2.write("forceplay " + userid + "\n")
                    heat2.write("broadcastto " + userid + "  You are in: "+racename2 + "\n")
                    heat2.close()
                    heat2warmup.write("broadcast ["+nc+"m " + username + "\n")
                    heat2warmup.write("forceplay " + userid + "\n")
                    heat2warmup.write("broadcastto " + userid + "  You are in: "+racename2 + "\n")
                    heat2warmup.close()
                    uidfile.write(userid+'\n')
                    uidfile.close()
                    y+=1
                x+=1

    #Appends the number of riders in the race at the bottom of each file            
    a=str(z)
    b=str(y)
    heat1script = open("C:\\Scripts\\"+h1+".mxs","a")
    heat1script.write("broadcast  "+"\n")
    heat1script.write("broadcast ["+tc+"m ---------------------------------------------------------------"+"\n")
    heat1script.write("broadcast ["+tc+"m "+a+" Riders"+"\n")
    heat1script.write("broadcast ["+tc+"m ---------------------------------------------------------------"+"\n")
    heat1script.write("broadcast  "+"\n")
    heat1script.close()
    heat2script = open("C:\\Scripts\\"+h2+".mxs","a")
    heat2script.write("broadcast  "+"\n")
    heat2script.write("broadcast ["+tc+"m ---------------------------------------------------------------"+"\n")
    heat2script.write("broadcast ["+tc+"m "+b+" Riders"+"\n")
    heat2script.write("broadcast ["+tc+"m ---------------------------------------------------------------"+"\n")
    heat2script.write("broadcast  "+"\n")
    heat2script.close()

    heat1warmup = open("C:\\Scripts\\"+h1+"warmup"+".mxs","a")
    heat1warmup.write("broadcast  "+"\n")
    heat1warmup.write("broadcast ["+tc+"m ---------------------------------------------------------------"+"\n")
    heat1warmup.write("broadcast ["+tc+"m "+a+" Riders"+"\n")
    heat1warmup.write("broadcast ["+tc+"m ---------------------------------------------------------------"+"\n")
    heat1warmup.write("broadcast  "+"\n")
    heat1warmup.close()
    heat2warmup = open("C:\\Scripts\\"+h2+"warmup"+".mxs","a")
    heat2warmup.write("broadcast  "+"\n")
    heat2warmup.write("broadcast ["+tc+"m ---------------------------------------------------------------"+"\n")
    heat2warmup.write("broadcast ["+tc+"m "+b+" Riders"+"\n")
    heat2warmup.write("broadcast ["+tc+"m ---------------------------------------------------------------"+"\n")
    heat2warmup.write("broadcast  "+"\n")
    heat2warmup.close()

    import subprocess
    subprocess.Popen('explorer "C:\Scripts"')

def heatsplitternos():
    #MXS Heat Splitter v4 by Sammy Holt

    #This script will take information from a lineup file and generate heat scripts to be run by mxserver
    #This version will use a signup file to parse the lineup file and only include the UIDS which are signed up
    #Make sure all output files are completely empty
    #Also, paste lineup file data into "lineuptest.txt"
    #DO NOT RENAME FILES, the script will fail
    #If you must change data, simply cut and paste into the designated file

    #Planned Features:
    #-Add a user defined amount of heats(In Progress)
    #-Easier file handling(Timeframe Unknown)

    #This section takes input from the user for initial file generation
    print "MX Simulator Heat Splitter No Signup Parsing"
    print "Created by Sammy Holt"
    print "\n"
    print "\n"

    lineup = raw_input('Drag and Drop your lineup file: ')
    h1 = raw_input('Enter a name for your heat 1 script: ')
    h2 = raw_input('Enter a name for your heat 2 script: ')
    racename = raw_input('Enter a race name for heat 1: ')
    racename2 = raw_input('Enter a race name for heat 2: ')
    uidname = raw_input('Enter a name for uid export: ')
    titlecolor = raw_input('Which color would you like the title to be?(Red,Green,Aqua,Blue,White,Black,Yellow,or Purple): ')
    namecolor = raw_input('Which color would you like the rider names to be?(Red,Green,Aqua,Blue,White,Black,Yellow,or Purple): ')
    tracknum = raw_input('What is the number of the track the race will be held on?: ')
    numLaps = raw_input('How many laps will these heats be?: ')
    numTransfer = raw_input('How many people transfer from each heat?: ')
    numWarmup = raw_input('Track number of warmup lap: ')

    num_lines = sum(1 for line in open(lineup))

    #Color Switcher for Title:
    #This block turns the user color string into a useable term
    if (titlecolor=='Red') or (titlecolor=='red'):
        tc='31'
    elif titlecolor=='Green' or (titlecolor=='green'):
        tc='32'
    elif titlecolor=='Aqua' or (titlecolor=='aqua'):
        tc='36'
    elif titlecolor=='Blue'  or (titlecolor=='blue'):
        tc='34'
    elif titlecolor=='White' or (titlecolor=='white'):
        tc='37'
    elif titlecolor=='Black' or (titlecolor=='black'):
        tc='30'
    elif titlecolor=='Yellow' or (titlecolor=='yellow'):
        tc='33'
    elif titlecolor=='Purple' or (titlecolor=='purple'):
        tc='35'
    else:
        print "Not a valid color"

    #Color Switcher for Names:
    #This block turns the user color string into a useable term
    if namecolor=='Red' or (namecolor=='red'):
        nc='31'
    elif namecolor=='Green' or (namecolor=='green'):
        nc='32'
    elif namecolor=='Aqua' or (namecolor=='aqua'):
        nc='36'
    elif namecolor=='Blue' or (namecolor=='blue'):
        nc='34'
    elif namecolor=='White' or (namecolor=='white'):
        nc='37'
    elif namecolor=='Black' or (namecolor=='black'):
        nc='30'
    elif namecolor=='Yellow' or (namecolor=='yellow'):
        nc='33'
    elif namecolor=='Purple' or (namecolor=='purple'):
        nc='35'
    else:
        print "Not a valid color"

    #Create a variable for half the amount of riders
    #halfriders = (amtRiders/2)

    #Generate the titles and some other settings for the two scripts                
    heat1script = open("C:\\Scripts\\"+h1+".mxs","a")
    heat2script = open("C:\\Scripts\\"+h2+".mxs","a")
    heat1warmup = open("C:\\Scripts\\"+h1+"warmup"+".mxs","a")
    heat2warmup = open("C:\\Scripts\\"+h2+"warmup"+".mxs","a")
    heat1nr = open("C:\\Scripts\\"+h1+"nr"+".mxs","a")
    heat2nr = open("C:\\Scripts\\"+h2+"nr"+".mxs","a")
    heat1nrwarmup = open("C:\\Scripts\\"+h1+"nrwarmup"+".mxs","a")
    heat2nrwarmup = open("C:\\Scripts\\"+h2+"nrwarmup"+".mxs","a")

    heat1script.write("#mxserverscript"+"\n")
    heat1script.write("broadcast ["+tc+"m ---------------------------------------------------------------"+"\n")
    heat1script.write("broadcast ["+tc+"m "+racename+" - "+numLaps+" Laps - "+"Top "+numTransfer+" Transfer"+"\n")
    heat1script.write("broadcast ["+tc+"m ---------------------------------------------------------------"+"\n")
    heat1script.write("allignore all"+'\n')
    heat1script.write("settrack "+tracknum+'\n')
    heat1script.write("forcespec all"+'\n')
    heat1script.close()

    heat2script.write("#mxserverscript"+"\n")
    heat2script.write("broadcast ["+tc+"m ---------------------------------------------------------------"+"\n")
    heat2script.write("broadcast ["+tc+"m "+racename2+" - "+numLaps+" Laps - "+"Top "+numTransfer+" Transfer"+"\n")
    heat2script.write("broadcast ["+tc+"m ---------------------------------------------------------------"+"\n")
    heat2script.write("allignore all"+'\n')
    heat2script.write("settrack "+tracknum+'\n')
    heat2script.write("forcespec all"+'\n')
    heat2script.close()

    #Warmup Files
    heat1warmup.write("#mxserverscript"+"\n")
    heat1warmup.write("broadcast ["+tc+"m ---------------------------------------------------------------"+"\n")
    heat1warmup.write("broadcast ["+tc+"m "+racename+" - "+"Warmup Lap"+"\n")
    heat1warmup.write("broadcast ["+tc+"m ---------------------------------------------------------------"+"\n")
    heat1warmup.write("forcespec all"+"\n")
    heat1warmup.write("settrack "+numWarmup+"\n")
    heat1warmup.close()
    heat2warmup.write("#mxserverscript"+"\n")
    heat2warmup.write("broadcast ["+tc+"m ---------------------------------------------------------------"+"\n")
    heat2warmup.write("broadcast ["+tc+"m "+racename2+" - "+"Warmup Lap"+"\n")
    heat2warmup.write("broadcast ["+tc+"m ---------------------------------------------------------------"+"\n")
    heat2warmup.write("forcespec all"+"\n")
    heat2warmup.write("settrack "+numWarmup+"\n")
    heat2warmup.close()

    #Next Race Script
    heat1nr.write("#mxserverscript"+"\n")
    heat1nr.write("broadcast  Next Race is: "+racename+"\n")
    heat1nr.close()
    heat1nrwarmup.write("#mxserverscript"+"\n")
    heat1nrwarmup.write("broadcast  Next Race is: "+racename+" Warmup Lap"+"\n")
    heat1nrwarmup.close()
    #Next Race Script2
    heat2nr.write("#mxserverscript"+"\n")
    heat2nr.write("broadcast  Next Race is: "+racename2+"\n")
    heat2nr.close()
    heat2nrwarmup.write("#mxserverscript"+"\n")
    heat2nrwarmup.write("broadcast  Next Race is: "+racename2+" Warmup Lap"+"\n")
    heat2nrwarmup.close()

    #Stuff used to split heats evenly
    p=0
    with open(lineup,"r") as f:
        data = f.readlines()

        for line in data:
            words=line.split()
            userid=words[1]
            p+=1

    nRacers = (p/2)

    #This section will deal with the actual lineup file
    x=0
    y=0
    z=0
    with open(lineup,"r") as f:
        data = f.readlines()

        for line in data:
            #Open files for output
            heat1=open("C:\\Scripts\\"+h1+".mxs","a")
            heat2=open("C:\\Scripts\\"+h2+".mxs","a")
            heat1warmup=open("c:\\Scripts\\"+h1+"warmup"+".mxs","a")
            heat2warmup=open("c:\\Scripts\\"+h2+"warmup"+".mxs","a")
            uidfile=open("c:\\Scripts\\"+uidname+".txt","a")
            #Take lineup file into a list
            words=line.split()
            
            #Make a slot variable
            slot=words[0]
            
            #Set a variable equal to names and uids of each slot
            #Also cleans up the underscores from the lineup file and replaces them with a whitespace
            userid=words[1]
            username=words[2].replace("_"," ")
            
            #The following will generate the heats (AKA. any bugs are probably here)
            if x<nRacers:
                heat1.write("broadcast ["+nc+"m " + username + "\n")
                heat1.write("forceplay " + userid + "\n")
                heat1.write("broadcastto " + userid + "  You are in: "+racename + "\n")
                heat1.close()
                heat1warmup.write("broadcast ["+nc+"m " + username + "\n")
                heat1warmup.write("forceplay " + userid + "\n")
                heat1warmup.write("broadcastto " + userid + "  You are in: "+racename + "\n")
                heat1warmup.close()
                uidfile.write(userid+"\n")
                uidfile.close()
                z+=1
            else:
                heat2.write("broadcast ["+nc+"m " + username + "\n")
                heat2.write("forceplay " + userid + "\n")
                heat2.write("broadcastto " + userid + "  You are in: "+racename2 + "\n")
                heat2.close()
                heat2warmup.write("broadcast ["+nc+"m " + username + "\n")
                heat2warmup.write("forceplay " + userid + "\n")
                heat2warmup.write("broadcastto " + userid + "  You are in: "+racename2 + "\n")
                heat2warmup.close()
                uidfile.write(userid+"\n")
                uidfile.close()
                y+=1
            x+=1

    #Appends the number of riders in the race at the bottom of each file            
    a=str(z)
    b=str(y)
    heat1script = open("C:\\Scripts\\"+h1+".mxs","a")
    heat1script.write("broadcast  "+"\n")
    heat1script.write("broadcast ["+tc+"m ---------------------------------------------------------------"+"\n")
    heat1script.write("broadcast ["+tc+"m "+a+" Riders"+"\n")
    heat1script.write("broadcast ["+tc+"m ---------------------------------------------------------------"+"\n")
    heat1script.write("broadcast  "+"\n")
    heat1script.close()
    heat2script = open("C:\\Scripts\\"+h2+".mxs","a")
    heat2script.write("broadcast  "+"\n")
    heat2script.write("broadcast ["+tc+"m ---------------------------------------------------------------"+"\n")
    heat2script.write("broadcast ["+tc+"m "+b+" Riders"+"\n")
    heat2script.write("broadcast ["+tc+"m ---------------------------------------------------------------"+"\n")
    heat2script.write("broadcast  "+"\n")
    heat2script.close()

    heat1warmup = open("C:\\Scripts\\"+h1+"warmup"+".mxs","a")
    heat1warmup.write("broadcast  "+"\n")
    heat1warmup.write("broadcast ["+tc+"m ---------------------------------------------------------------"+"\n")
    heat1warmup.write("broadcast ["+tc+"m "+a+" Riders"+"\n")
    heat1warmup.write("broadcast ["+tc+"m ---------------------------------------------------------------"+"\n")
    heat1warmup.write("broadcast  "+"\n")
    heat1warmup.close()
    heat2warmup = open("C:\\Scripts\\"+h2+"warmup"+".mxs","a")
    heat2warmup.write("broadcast  "+"\n")
    heat2warmup.write("broadcast ["+tc+"m ---------------------------------------------------------------"+"\n")
    heat2warmup.write("broadcast ["+tc+"m "+b+" Riders"+"\n")
    heat2warmup.write("broadcast ["+tc+"m ---------------------------------------------------------------"+"\n")
    heat2warmup.write("broadcast  "+"\n")
    heat2warmup.close()

    import subprocess
    subprocess.Popen('explorer "C:\Scripts"')
This is what it creates:
[Image: 4Omyp7f]
This is an example of what it should create:
Output:
#mxserverscript broadcast  --------------------------------------------------------------- broadcast  Heat 2 - 8 Laps - Top 4 Transfer broadcast  --------------------------------------------------------------- allignore all settrack 1 forcespec all broadcast  Lochlan Berg | FGR forceplay 28971 broadcastto 28971  You are in: Heat 2 broadcast broadcast  --------------------------------------------------------------- broadcast  1 Riders broadcast  --------------------------------------------------------------- broadcast
And this is what it creates now:
Output:
#mxserverscript broadcast  --------------------------------------------------------------- broadcast  Heat 2 - 8 Laps - Top 4 Transfer broadcast  --------------------------------------------------------------- allignore all settrack 1 forcespec all broadcast  Berg forceplay 28971 broadcastto 28971  You are in: Heat 2 broadcast broadcast  --------------------------------------------------------------- broadcast  1 Riders broadcast  --------------------------------------------------------------- broadcast
Reply
#2
Why are you using raw_input? Use input, unless you are using python 2.7. Then, I would recommend using python 3
pyzyx3qwerty
"The greatest glory in living lies not in never falling, but in rising every time we fall." - Nelson Mandela
Need help on the forum? Visit help @ python forum
For learning more and more about python, visit Python docs
Reply
#3
I tried to refactor you code. It's hared.
First you use Python 2.7.
I guess you also don't know format strings, context managers and pathlib.

Here what I've done so far:
Keep in mind, that some methods are wrong. The code is not working.
For example you append text to your files. I replaced the open files with Path objects, so the open Method must be used.

Some variables can created with a mapping (dict).
    color_mapping = dict(
        zip(
            ["red", "green", "aqua", "blue", "white", "black", "yellow", "purple"],
            count(31),
        )
    )
Repetitive code should be moved into functions.
For example you're creating from variables the Paths and then you open the paths, then you append text to them.
This could be splitted into 3 functions. get_paths, create_text, write_text_to_files.

def get_paths(h1, h2):
    scripts = Path("C://Scripts")
    h1_warmup = h1 + "warmup"
    h2_warmup = h2 + "warmup"
    h1_nr = h1 + "nr"
    h2_nr = h2 + "nr"
    h1_nrwarmup = h1 + "nrwarmup"
    h2_nrwarmup = h1 + "nrwarmup"

    heat1script = (scripts / h1).with_suffix(".mxs")
    heat2script = (scripts / h2).with_suffix(".mxs")
    heat1warmup = (scripts / h1_warmup).with_suffix(".mxs")
    heat2warmup = (scripts / h2_warmup).with_suffix(".mxs")
    heat1nr = (scripts / h1_nr).with_suffix(".mxs")
    heat2nr = (scripts / h2_nr).with_suffix(".mxs")
    heat1nrwarmup = (scripts / h1_nrwarmup).with_suffix(".mxs")
    heat2nrwarmup = (scripts / h2_nrwarmup).with_suffix(".mxs")
    return (
        heat1nr,
        heat1nrwarmup,
        heat1script,
        heat1warmup,
        heat2nr,
        heat2nrwarmup,
        heat2script,
        heat2warmup,
    )
This returns only paths.
Another function could prepare the text.

If you use more functions, you code will be less repetitive.
Sometimes you can't avoid it, but in the most cases it's a sign for a bad program structure.

If you open your code in a modern IDE like PyCharm you get warnings over warnings in all colors.
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  receive from a generator, send to a generator Skaperen 9 5,527 Feb-05-2018, 06:26 AM
Last Post: Skaperen

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020