473,408 Members | 1,830 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,408 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 1416
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: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.