I don't even know what it's called...

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Thomascountz
    New Member
    • Jul 2008
    • 6

    I don't even know what it's called...

    I'm completely new to python, I just started about 3 days ago. I've requested some books from my library on python, so I hope I'm headed in the right direction. I hate to ask this because I'm sure it's been asked before, but I don't know what it is called so I don't even know where to begin. Is it possible to edit the code of a python program, while executing it.

    Code:
    if loop == 2:    
        def menu(list,question):
            for entry in list:
                print 1+list.index(entry),
                print ") " + entry
    
            return input (question) - 1
    
    CEF = ['CURRENT OBJECTIVES','CURRENT TARGET','CURRENT MISSION','Exit']
    
    name = raw_input("Please insert your name: ")
    print "Hello", name,"Welcome to the Information Desk"
    print ""
    while loop == 2:
        choice = menu(CEF, "Please Select: ")
        if choice == 0:
            Print "There is no information"
        if choice == 1:
            print "There is no information"
        if choice == 2:
            print "There is no information"
        if choice == 3:
            print "Thank You",name
            sleep(2)
            loop = 4
    I want to change the places where it says "there is no information" to whatever I want, without having to edit the code manually, instead I would like to enter the information with the program itself. Almost like changing the value of a variable, except, making it permanent, so it will be like that next time I run the program.

    Again, sorry if this has already been asked, but What is this called and How do I do it?
  • jlm699
    Contributor
    • Jul 2007
    • 314

    #2
    Originally posted by Thomascountz
    Code:
    if loop == 2:    
        def menu(list,question):
            for entry in list:
                print 1+list.index(entry),
                print ") " + entry
    
            return input (question) - 1
    
    CEF = ['CURRENT OBJECTIVES','CURRENT TARGET','CURRENT MISSION','Exit']
    
    name = raw_input("Please insert your name: ")
    print "Hello", name,"Welcome to the Information Desk"
    print ""
    while loop == 2:
        choice = menu(CEF, "Please Select: ")
        if choice == 0:
            Print "There is no information"
        if choice == 1:
            print "There is no information"
        if choice == 2:
            print "There is no information"
        if choice == 3:
            print "Thank You",name
            sleep(2)
            loop = 4
    A possibility would be to initialize variables before your while loop. Let's say:
    choice0 = choice1 = choice2 = choice3 = 'There is no information'
    Then during the loop you simply put print choice0 or print choice1...

    During the loop Then you could store some kind of input with:
    usr_input = raw_input('Ente r some text: ')

    Then you could assign one of your choices to the same value like
    choice0 = usr_input

    This might be what you're looking for. There's not really any specific name for this, it's just dynamic variables.

    Comment

    • Thomascountz
      New Member
      • Jul 2008
      • 6

      #3
      That's not exactly what I was wondering how to do. Let me rephrase:

      Lets say I have a variable: a
      in the code it would be written:
      a = 0
      When I run the code: print a
      The output would be: 0
      When running the program, I tell it: a = 1
      in the code it would now be written:
      a = 1

      The actually code has changed.
      How would I do this, if it is possible.

      hope this makes sense.

      Comment

      • Dekudude
        New Member
        • Jul 2008
        • 9

        #4
        You want it to remain as "a = 1" inside the actual code, so if you were to open it in Notepad, you would see "a = 1"?

        Comment

        • Thomascountz
          New Member
          • Jul 2008
          • 6

          #5
          Yes. Exactly...is that possible?

          Comment

          • jlm699
            Contributor
            • Jul 2007
            • 314

            #6
            You could open the .py file, scan it, find the variable and then replace the value with some simple string manipulation.

            Comment

            • Thomascountz
              New Member
              • Jul 2008
              • 6

              #7
              how do you scan for the variables?

              Comment

              • jlm699
                Contributor
                • Jul 2007
                • 314

                #8
                Originally posted by Thomascountz
                how do you scan for the variables?
                You would have to write your own utility for parsing the file, but it would probably be something like this:
                [code=python]
                f = open( my_prog.py, 'r' )

                for line in f:
                data = line.split('=')
                if len( data ) == 2:
                var_name = data[0]
                try:
                var_val = int( data[1] )
                # Now you can change the value to whatever you want
                except:
                # Either a non-integer value or a string
                pass[/code]
                Simple file reading/writing and basic text parsing is all it is.

                Comment

                Working...