472,958 Members | 2,515 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,958 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 1408
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

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

Similar topics

1
by: J. Kenney | last post by:
Good Morning, Is there a way for a function called within an _imported_ library to call back to _calling_ python program to run a function? (Shown below) Also can you overload the print...
12
by: qwweeeit | last post by:
The pythonic way of programming requires, as far as I know, to spread a big application in plenty of more manageable scripts, using import or from ... import to connect the various modules. In...
2
by: rob | last post by:
Hi all, I've got a csv file which I will need to import into an access db via asp. The csv file will be imported every day of each month so there will be duplicates which will not need to be...
0
by: Michael | last post by:
I apologize if this might be a beginner question, which is clear to the experts. But I could not find a clear answer yet. I have extracted a schema / user with the exp utility. I want to import...
10
by: Derek van Vliet | last post by:
Using the Python/C API, is there a way I can import a pyc file that I have in memory (as opposed to loading from disk)? I'm trying to save compiled python code in a proprietary file format to...
12
by: expect | last post by:
What's the big deal with importing text files? I have a 70 MB file to import and it's been one problem after another. I used the copy command and it appears that it's just not possible. I finally...
4
by: Sujeet | last post by:
Hey guys, I want to import a database from SQL Server 2000 to MS Access with all indices and keys along with the tables. DTS does not export indices and keys, only the structure and the data. ...
3
by: Sam Alexander | last post by:
Hi Everone, I'm writing a script to import data from an XML file, and this tutorial is really an awesome guide : http://www.kbalertz.com/Q316005/Import.Server.Component.aspx ... problem though...
0
by: fboweb.com | last post by:
Hey there! I have a DTS import package from an old SQL server that takes flat text files, does some munging on the columns using ActiveX scripts and imports those flat files to various tables. ...
0
by: norseman | last post by:
mercado mercado wrote: =================================== I started to import a module using its path and now see what you mean. Python is missing the concept: Programmer dictates what machine...
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
2
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...

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.