Running your program
Each project has a main.py file. Pressing Run runs it, and what your program prints appears in the console beside the code.
For now you can only create Console projects, which print text and ask questions. The Pygame Zero and Pygame examples on the home page show games running, and creating games of your own is coming soon.
Loading Python
When you open pygwide, it starts loading Python straight away. A bar at the top of the page shows how far it has got, such as Loading Python… 40%, then Starting Python…. The bar goes away once Python is ready.
The first time, this can take a little while, because Python is a big download. After that your browser keeps a copy, so it is much quicker.
You don't have to wait. You can write code while Python loads, and if you press Run before it is ready, your program starts as soon as Python is ready.
If loading fails, perhaps because the wifi dropped, the bar says:
Python could not be loaded. Check the connection, then press Try again.
Press Try again to load it again. If nothing arrives for 30 seconds, pygwide counts the load as failed, so you are never left waiting for ever.
Run and Stop
| To do this | Do this |
|---|---|
| Run your program | Press ▶ Run, or Ctrl+Enter wherever you are on the page |
| Stop it | Press ■ Stop, which takes Run's place while your program runs |
| Run it again | Press Run again. Each run starts afresh with an empty console. |
Your changes are saved before your program starts, so even a program that freezes the page cannot lose them.
Stop ends a Console program within a second, even one stuck in a loop that never ends. Going back to My Projects stops a running program too.
What the status says
Next to the Run button, a few words say what your program is doing:
| Status | What it means |
|---|---|
| Ready | Nothing has run yet. |
| Loading Python | You pressed Run before Python was ready. Your program starts as soon as it is. |
| Running | Your program is running. |
| Waiting for your answer | Your program is waiting for you to type something. |
| Finished | Your program has ended. |
| Stopped | You pressed Stop. |
| Did not finish | Something went wrong before your program could finish, such as Python not loading. The console says what happened. |
When your program ends, the console also says Program finished or Program stopped on a line of its own.
Answering questions
When your program calls input(), a box appears in the console, straight
after the question, saying Type your answer, then press Enter. Type
your answer and press Enter. Your answer stays in the console, just as
it would on a computer.
name = input("What is your name? ")
print("Hello, " + name + "!")
Pressing Ctrl+D in the answer box ends the input instead of answering.
Your program then gets an EOFError, as it would on a computer, so a
program can read lines until you press Ctrl+D:
try:
while True:
line = input()
print("You typed", line)
except EOFError:
print("No more lines")
Where input() cannot be used
A web page cannot stop and wait the way a computer can, so pygwide changes
each input() before your program runs. This works in your program's main
code, in functions, in methods, and in loops, in main.py and in any other
Python file of your project that it imports. It does not work in a few
places:
- special methods, such as
__init__ - generators, which are functions that use
yield - lambdas
- functions with a decorator other than
@staticmethodor@classmethod - the body of a class itself, outside its methods
- the top of a file your program imports, outside its functions, because that code runs while the file is being imported. Put the question in a function, and call it from your program.
Using input() in one of these gives this error at that line:
input() cannot be used in this part of a program. Ask for the answer elsewhere, and pass it in.
So ask the question somewhere else, such as before creating an object, and pass the answer in.
Files your program uses
Your program can use the other files in its project, as it would on a computer.
- Modules.
import helperfindshelper.pyin your project, andfrom levels import onefindslevels/one.py. - Reading and writing files.
open("highscore.json")opens a file in your project. A file your program creates or changes is kept once the program ends, so a high score is still there next time, and it goes into your exports. - Some files are not kept. A project can only hold Python, pictures, sounds, fonts, and text and data files, with names that work on every computer. If your program writes any other file, it is not kept, and the console says so.
main.pyalways comes back. If your program deletesmain.py, an empty one is put back, and the console says so.- Pressing Stop. The files your program wrote and closed, and the
folders it made, before you pressed Stop are kept, whether it is a
Console program or a game. But
Stop ends a Console program at once, so a file it still had open, part
written, is not kept. Use
with open(...), and the file is closed as soon as you have finished writing it. - Examples never change. If you run an example and its program writes a file, pygwide copies the example into My Projects for you, and the file is kept in your copy. Try Calendar, which saves a calendar for any year, or High scores, which keeps a table of your best scores.
When something goes wrong
Errors appear in the console in a different colour from your program's
own output, so you can spot them. The error starts from your own code,
naming your file and line, such as main.py, line 3, without any of
pygwide's own workings.
exit(), quit() and sys.exit() simply finish your program, as they do
on a computer. A message given to sys.exit() is shown in the console.
Very occasionally Python itself stops working, such as when a program uses up all its memory. The console then says:
Python stopped working, perhaps because the program used up all its memory. Python has been started again, so you can press Run.
Your code is still there. Fix the problem, such as a list that grows for ever, and press Run again.