-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b7255f5
commit d95da36
Showing
2 changed files
with
63 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
{ | ||
"description": { | ||
"names": { | ||
"en": "Scripting" | ||
} | ||
}, | ||
"labels": [ | ||
"real-world programming" | ||
], | ||
"internals": { | ||
"token": "X5rVj9e4R7tbj4SxX78RXrS93eTq5CkKfT6HWTJrer2I-N33yqry9dj-nSRfzHct", | ||
"_info": "These fields are used for internal bookkeeping in Dodona, please do not change them." | ||
} | ||
} |
49 changes: 49 additions & 0 deletions
49
dodona/reading-exercises/scripts/description/description.en.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
When you write a program for yourself (or download a Python program from the internet), you will | ||
probably want to run it sooner or later. | ||
|
||
This is quite straightforward. You can simply follow these steps: | ||
* Open the terminal program you use (on Windows this might be called "Power Shell" or "CMD"), | ||
* Navigate to the folder where the program is stored, | ||
* Run the program by typing `python my_cool_program.py` (also known as running a "script"). | ||
|
||
There is an important problem when building programs that you should be aware of: any top-level code | ||
always runs when you run the script **BUT ALSO** at _import_ time. That's right! You can `import` | ||
your program too. | ||
|
||
This is a problem because, you probably don't expect to be running Python code just because you | ||
`import`ed something. To avoid this you can use a trick to check if your code is being run as a script | ||
or if it's being imported. | ||
|
||
There is a special variable that Python gives you called `__name__` which is the name of the current | ||
module. Try this in your own programming environment if you like: create 2 files. | ||
|
||
A file "a.py" should contain: | ||
```python | ||
print("__name__ is: ", __name__) | ||
``` | ||
|
||
A file "b.py" should contain: | ||
```python | ||
import a | ||
print("I'm in b") | ||
``` | ||
|
||
Now run `python a.py` (Use a.py as a script, not importing it). This should be printed to the console: | ||
``` | ||
__name__ is: __main__ | ||
``` | ||
|
||
And if you run `python b.py` (Import a.py). This should print: | ||
``` | ||
__name__ is: a | ||
I'm in b | ||
``` | ||
|
||
Notice that the special variable `__name__` contains `"__main__"` when you run the file as a script. | ||
So when you define code to run when using your program as a script you should use this construction: | ||
```python | ||
if __name__ == "__main__": | ||
# code to run when using my program as a script. | ||
|
||
``` | ||
|