471,334 Members | 2,221 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,334 software developers and data experts.

Importing other scripts?

I am working on a game and I was wondering if I could make a script called HighScores. What this would do is that it would take the score the user had gotten, enter it into a list, sort the list, and then display the top ten scores. However, instead of copying this code into any game I make I was wondering if in the game I could type a line of code to import HighScores.py and use it like a function.

Thanks for the help,

Jordan
Jul 15 '07 #1
4 1255
ilikepython
844 Expert 512MB
I am working on a game and I was wondering if I could make a script called HighScores. What this would do is that it would take the score the user had gotten, enter it into a list, sort the list, and then display the top ten scores. However, instead of copying this code into any game I make I was wondering if in the game I could type a line of code to import HighScores.py and use it like a function.

Thanks for the help,

Jordan
All you need to do is this:
Expand|Select|Wrap|Line Numbers
  1. import HighScores
  2.  
  3. score = 263  # sample score
  4. HighScores.proccesScore(score)  # call the functions you have written in HighScores.py
  5.  
Is that what you need?
Jul 15 '07 #2
bartonc
6,596 Expert 4TB
I am working on a game and I was wondering if I could make a script called HighScores. What this would do is that it would take the score the user had gotten, enter it into a list, sort the list, and then display the top ten scores. However, instead of copying this code into any game I make I was wondering if in the game I could type a line of code to import HighScores.py and use it like a function.

Thanks for the help,

Jordan
My friend, ilikepython, has given the "cleanest" example of the import function.
I'll include some others so that you will know what they do when you see them or use them yourself.
This syntax allows you to leave off the 'module.' part:
Expand|Select|Wrap|Line Numbers
  1. from myModule import myFunction # Note that you must leave off the (.py, usually) extension
  2. myFunction(whatever_is_defined_in_myModule) # Python is all about names, name spaces and scopes
  3. # With no indentation, all assignment of names happens in the module (global, if you must) scope
  4. # So, as you can see, the name 'myFunction' was imported into this module's namespace
This syntax is discouraged, as most consider it to be a good way to clutter up your module's namespace.
Expand|Select|Wrap|Line Numbers
  1. from LibraryModule import *  # import every single name defined in that module into this one (seen quite often in Tkinter programs).
One catch to watch out for is using these in interactive mode in the interpreter:
Once you have imported a name in an interactive session, the interpreter stubbornly holds onto it. If you go and make a change to the imported module, you must use
Expand|Select|Wrap|Line Numbers
  1. reload(myModule)
in order to get the updated function, etc.

I hope that's not too confusing....
Jul 15 '07 #3
Thank you both for your help.

ilikepython- that was exactly what I was looking for
bartonc- you confused me a bit but after reviewing the code I realized what you were saying

Also, I have a list of 11 entries called "scores"
By calling the process() function with the parameter score, the function takes the score variable and places it in scores[10]. I then sort the list and display scores[0] thru scores[9]. However, only zeros show and the "score" variable, being 5, does not show. I tried using scores.reverse, but nothing happened. Please help.

Thank you for your help,
Jordan
Jul 15 '07 #4
bartonc
6,596 Expert 4TB
Thank you both for your help.

ilikepython- that was exactly what I was looking for
bartonc- you confused me a bit but after reviewing the code I realized what you were saying

Also, I have a list of 11 entries called "scores"
By calling the process() function with the parameter score, the function takes the score variable and places it in scores[10]. I then sort the list and display scores[0] thru scores[9]. However, only zeros show and the "score" variable, being 5, does not show. I tried using scores.reverse, but nothing happened. Please help.

Thank you for your help,
Jordan
Hi Jordan. I'll bet that we can help with your processing function. 2 things:
Please start a new discussion with an appropriate title.
Please read the posting guidelines on the right hand side (to learn how to use [ CODE ] tags) while doing so.

Thanks you so much.
Jul 15 '07 #5

Post your reply

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

Similar topics

1 post views Thread by J. Kenney | last post: by
12 posts views Thread by qwweeeit | last post: by
10 posts views Thread by Derek van Vliet | last post: by
12 posts views Thread by expect | last post: by
reply views Thread by norseman | last post: by
reply views Thread by rosydwin | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.