# save this code as dec2bin.py so you can access the function decimal2binary()
# in other Python programs
'''convert decimal (base 10) integer n to binary (base 2) string bStr'''
bStr = ''
if n < 0: raise ValueError, "must be a positive"
if n == 0: return '0'
while n > 0:
bStr = str(n % 2) + bStr
n = n >> 1
# This test runs when used as a standalone program, but not as an imported module.
# Let's say you save this module as dec2bin.py and use it in another program
# when you import dec2bin the __name__ namespace would now be dec2bin and the
# test would be ignored
if __name__ == '__main__':
print decimal2binary(255) # test result = 11111111
... and here is how you would use it ...
# using your custom made module dec2bin.py
# put it the same folder so Python can find it
print dec2bin.decimal2binary(128) # 10000000
What I want to do is make a program that shows a list, like say:
 --- Guess Number Game
 --- Calendar
And I code the guess number game and calendar as seperate .py programs. I would like to make a program that shows that list and when the user picks one, the program will start to run that program.
I would just make the code be all together in one big program. But I am just learning to program in python and I personally find it more organised to call txt files instead of having huge paragraphs in the code, and would also like to make seperate programs that can be called on. I find it easier to keeop track of everything.
Another thing I need help with, is how do i get the program to loop back the begining at the end
Here is a way suggested in a different thread.
You put program code in a loop and designate one key to break out of loop:
# this is an example of your program:
print "My program! Really!"
choice = raw_input("Enter r (repeat) or q (quit): ")
if 'q' in choice:
break # exits the while loop, any other key repeats loop
This one uses the option menu within a infinite while loop with a quit option to break out of the loop. The games are called with functions.
Instead of functions you can use execfile("mygame.py") to execute external Python programs you have written and saved in the same directory. If they are in different directories, you have to give the full path.
when i was first playing about with python (wait... i still am) anyway, a good website is: http://www.bembry.org/
just click on the python section, it has a great simple pdf tut and some quick notes aswell