By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
464,563 Members | 905 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 464,563 IT Pros & Developers. It's quick & easy.

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

P: 6
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.

Expand|Select|Wrap|Line Numbers
  1. if loop == 2:    
  2.     def menu(list,question):
  3.         for entry in list:
  4.             print 1+list.index(entry),
  5.             print ") " + entry
  6.  
  7.         return input (question) - 1
  8.  
  9. CEF = ['CURRENT OBJECTIVES','CURRENT TARGET','CURRENT MISSION','Exit']
  10.  
  11. name = raw_input("Please insert your name: ")
  12. print "Hello", name,"Welcome to the Information Desk"
  13. print ""
  14. while loop == 2:
  15.     choice = menu(CEF, "Please Select: ")
  16.     if choice == 0:
  17.         Print "There is no information"
  18.     if choice == 1:
  19.         print "There is no information"
  20.     if choice == 2:
  21.         print "There is no information"
  22.     if choice == 3:
  23.         print "Thank You",name
  24.         sleep(2)
  25.         loop = 4
  26.  
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?
Jul 1 '08 #1
Share this Question
Share on Google+
7 Replies

jlm699
100+
P: 314
Expand|Select|Wrap|Line Numbers
  1. if loop == 2:    
  2.     def menu(list,question):
  3.         for entry in list:
  4.             print 1+list.index(entry),
  5.             print ") " + entry
  6.  
  7.         return input (question) - 1
  8.  
  9. CEF = ['CURRENT OBJECTIVES','CURRENT TARGET','CURRENT MISSION','Exit']
  10.  
  11. name = raw_input("Please insert your name: ")
  12. print "Hello", name,"Welcome to the Information Desk"
  13. print ""
  14. while loop == 2:
  15.     choice = menu(CEF, "Please Select: ")
  16.     if choice == 0:
  17.         Print "There is no information"
  18.     if choice == 1:
  19.         print "There is no information"
  20.     if choice == 2:
  21.         print "There is no information"
  22.     if choice == 3:
  23.         print "Thank You",name
  24.         sleep(2)
  25.         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('Enter 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.
Jul 1 '08 #2

P: 6
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.
Jul 4 '08 #3

P: 9
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"?
Jul 4 '08 #4

P: 6
Yes. Exactly...is that possible?
Jul 4 '08 #5

jlm699
100+
P: 314
You could open the .py file, scan it, find the variable and then replace the value with some simple string manipulation.
Jul 7 '08 #6

P: 6
how do you scan for the variables?
Jul 11 '08 #7

jlm699
100+
P: 314
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:
Expand|Select|Wrap|Line Numbers
  1. f = open( my_prog.py, 'r' )
  2.  
  3. for line in f:
  4.     data = line.split('=')
  5.     if len( data ) == 2:
  6.         var_name = data[0]
  7.         try:        
  8.             var_val = int( data[1] )
  9.             # Now you can change the value to whatever you want
  10.         except:
  11.             # Either a non-integer value or a string
  12.             pass
Simple file reading/writing and basic text parsing is all it is.
Jul 11 '08 #8

Post your reply

Sign in to post your reply or Sign up for a free account.