Domanda

I am needing help on some code I have been working on.

def themain():
    print "============================================================================"
    lol = raw_input("""Would you like to add new stock prices to a company (add)/read all of the previous
stock prices(read)(note that the newest stock price will be at the far right while
the oldest stock price will be located at the far left)/or add a new company(add new): """)
    if lol.lower() == "add":
        def main():
            print "============================================================================"
            lol = raw_input("Insert Name of Company: ")
            print "Are you sure you typed the company name in right? Answer with yes or no. : "  + lol
            lol2 = raw_input()
            if lol2 == "yes":
                fo = open(lol.lower() + ".txt", "a")
                main = raw_input("Type in new stock price: ")
                fo.write(main + ":");
                fo.close()
                themain()
            elif lol2 == "no":
                main()
            else:
                print "Please type either yes or no"
                themain()
        main()
    elif lol.lower() == "read":
        def main2():
                def read():
                    print "============================================================================"
                    lol = raw_input("Insert Name of Company: ")
                    try:
                        fo = open(lol.lower() + ".txt", "r+")
                        read = fo.read();
                        print read
                        fo.close()
                    except IOError:
                        print "Not a valid company please try again"
                        main2()
                    themain()
                read()
        main2()
    elif lol.lower() == "add new":
        def addnew():
            print "============================================================================"
            lol = raw_input("Insert the name of the new company: ")
            print "Are you sure you typed in the name correctly?"
            print lol
            print "Please say either yes or no: "
            response = raw_input()
            if response.lower() == "yes":
                fo = open(lol.lower() + ".txt", "a+")
                fo.write(lol.lower + ":")
                fo.close()
                themain()
            elif response.lower() == "no":
                addnew()
            else:
                print "Please say either yes or no"
                themain()
        addnew()
    else:
        print "Please say either read, add new, or add"
        themain()
themain()

Thought it might be easier to explain after I paste my code. So at the moment when you make a new business it creates the file and adds the business name to the front of the file. Then after that when you add new stocks to the business it appends them to the end. So for example it puts this into the text file Business:oldeststock:newerstock:neweststock . What I am trying to do is make it go in this order Business:neweststock:newerstock:oldeststock. I can't seem to find how I could do that. I tried seeking but it didn't turn out well when I did this to main()

def main():
        print "============================================================================"
        lol = raw_input("Insert Name of Company: ")
        print "Are you sure you typed the company name in right? Answer with yes or no. : "  + lol
        lol2 = raw_input()
        if lol2 == "yes":
            fo = open(lol.lower() + ".txt", "r+")
            main = raw_input("Type in new stock price: ")
            fo.seek(len(fo.name) - 4)
            fo.write(":" + main);
            fo.close()
            themain()
        elif lol2 == "no":
            main()
        else:
            print "Please type either yes or no"
            themain()
    main()

What I did was add fo.seek(len(fo.name) -4 ) so it would seek to the end of the name minus the extension (.txt) but all it does is replace the numbers that were already there. Is there any way I could add to it but from that point so it moves down the other numbers and inserts it there or am I completely thinking about this wrong and there is a simple solution that has alluded me for three hours (;-;) .

È stato utile?

Soluzione

Instead of opening the text file in "r+" mode, would opening it in "a+" mode and writing to the file be an option?

So that would look like this:

#Editing the if condition only

 if lol2 == "yes":
   #Indentation is off for some reason, this should be indented below the if statement

    fo = open(lol.lower() + ".txt", "r+")
    string = fo.read()
    fo.close()

    # Finding where the colon is located
    index = string.find(":")
    # Slicing where the :new:old is
    aStr = string[index:]
    main = raw_input("Type in new stock price: ")
    # Putting the string slices together
    main = string[:index]+":"+userinput+aStr
    fo = open("test.txt","w+")
    fo.write(main) #This would add the raw input at the end of the file.          
    fo.close()
    themain()
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top