Skip to content

Commit c48e795

Browse files
committed
reorganizing stuff
1 parent 90b07d5 commit c48e795

8 files changed

+168
-317
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@ it.**
2828
3. [Getting started with Python](getting-started.md)
2929
4. [ThinkPython: The way of the program](the-way-of-the-program.md)
3030
5. [Variables, Booleans and None](variables.md)
31-
6. [Using functions and storing code in files](using-functions.md)
32-
7. [If, elif, else and while](if.md)
31+
6. [Using functions](using-functions.md)
32+
7. [If, else and elif](if.md)
3333
8. [ThinkPython: Lists](lists.md)
3434

3535
Other things this tutorial comes with:

answers.md

Lines changed: 38 additions & 146 deletions
Original file line numberDiff line numberDiff line change
@@ -2,101 +2,55 @@
22

33
These are answers for exercises in the chapters. In programming, there's always more than one way to do things, so if your solution wasn't exactly like mine it's not necessarily wrong. Some Python users say that there should be only one right way, but that goal will never be fully reached.
44

5-
## Chapter 1
5+
## ThinkPython: The way of the program
66

7-
1. 18996.20 €
7+
1. The strings get added together.
8+
2. We get an error.
9+
3. We get a floating point number.
810

9-
```py
10-
>>> 49.95 + 200
11-
249.95
12-
>>> 100 * 249.95
13-
24995.0
14-
>>> 1 - (24 / 100)
15-
0.76
16-
>>> 24995.0 * 0.76
17-
18996.2
18-
>>>
19-
```
20-
21-
All in one line:
22-
23-
```py
24-
>>> 100 * (49.95 + 200) * (1 - (24 / 100))
25-
18996.2
26-
>>>
27-
```
28-
29-
2. About 523
30-
31-
```py
32-
>>> (4 / 3) * 3.14 * (5 * 5 * 5)
33-
523.3333333333334
34-
>>> 4 / 3 * 3.14 * 5 * 5 * 5 # parentheses aren't needed
35-
523.3333333333334
36-
>>>
37-
```
38-
39-
More advanced way:
40-
41-
```py
42-
>>> from math import pi
43-
>>> 4 / 3 * pi * 5 ** 3
44-
523.5987755982989
45-
>>>
46-
```
47-
48-
Spaces don't effect the calculating order, but you can use them to make the order easier to see:
49-
50-
```py
51-
>>> 4/3 * pi * 5**3
52-
523.5987755982989
53-
>>>
54-
```
55-
56-
## Chapter 3
57-
58-
1. Content of the file:
11+
## Using if, else and elif
5912

13+
1.
6014
```py
6115
word = input("Enter a word: ")
6216
print(word * 1000)
6317
```
6418

65-
2. Content of the file:
66-
19+
2.
6720
```py
6821
word = input("Enter a word: ")
6922
word += " "
7023
print(word * 1000)
7124
```
7225

73-
You can also add the space right away on the first line:
26+
We can also add the space right away on the first line:
7427

7528
```py
7629
word = input("Enter a word: ") + " "
7730
print(word * 1000)
7831
```
7932

80-
Of course, there are 999 spaces between 1000 words and this will print 1000 spaces instead, so there will be a useless space at the end, but it doesn't matter. To get rid of the space, you can do something like this instead:
33+
Of course, there are 999 spaces between 1000 words and this will
34+
print 1000 spaces instead, so there will be a useless space at the
35+
end, but it doesn't matter. If we really want to get rid of the
36+
space, we can do something like this instead:
8137

8238
```py
8339
no_space = input("Enter a word: ")
8440
yes_space = no_space + " "
8541
print(yes_space * 999 + no_space)
8642
```
8743

88-
3. Like this:
89-
44+
3.
9045
```py
9146
first = input("Enter a word: ")
9247
second = input("Enter another word: ")
9348
words = first + " " + second + " "
9449
print(words * 1000)
9550
```
9651

97-
## Chapter 4
98-
99-
1. You can compare the word against an empty string (`""` or `''`). In this example, the password is "secret".
52+
4. You can compare the word against an empty string (`""` or `''`) to
53+
check if it's empty. In this example, the password is "secret".
10054

10155
```py
10256
word = input("Enter your password: ")
@@ -109,7 +63,8 @@ These are answers for exercises in the chapters. In programming, there's always
10963
print("Access denied.")
11064
```
11165

112-
2. Simply check the username first, then the password in indented blocks of code.
66+
5. Simply check the username first, then the password in indented
67+
blocks of code.
11368

11469
```py
11570
username = input("Enter your username: ")
@@ -131,93 +86,30 @@ These are answers for exercises in the chapters. In programming, there's always
13186

13287
Example output:
13388

134-
>>> ================================ RESTART ================================
135-
>>>
136-
Enter your username: foo
137-
Enter your password: biz
138-
Welcome foo!
139-
>>> ================================ RESTART ================================
140-
>>>
141-
Enter your username: bar
142-
Enter your password: baz
143-
Welcome bar!
144-
>>> ================================ RESTART ================================
145-
>>>
146-
Enter your username: spam
147-
Enter your password: eggs
148-
Wrong username.
149-
>>> ================================ RESTART ================================
150-
>>>
151-
Enter your username: foo
152-
Enter your password: lol
153-
Wrong password!
154-
>>>
155-
156-
3. This is a great chance to use a while loop. In this example, the
157-
correct password is "secret".
158-
159-
```py
160-
running = True
161-
while running:
162-
password = input("Enter your password: ")
163-
if password == "secret":
164-
print("Welcome!")
165-
running = False
166-
else:
167-
print("Wrong password.")
168-
```
169-
170-
You can also use `break` to get out of a loop, and `while True:` to
171-
do an infinite loop. This is recommended instead of the previous
172-
alternative, because you don't need a useless `running` variable.
173-
174-
```py
175-
while True:
176-
password = input("Enter your password: ")
177-
if password == "secret":
178-
print("Welcome!")
179-
break
180-
else:
181-
print("Wrong password.")
182-
```
183-
184-
Even shorter alternative:
185-
186-
```py
187-
while input("Enter your password: ") != "secret":
188-
print("Wrong password.")
189-
print("Welcome!")
19089
```
191-
192-
4. One way to do this is to put the inputs directly to `if` and `elif`
193-
lines. Again, the correct password is "secret".
194-
195-
```py
196-
if input("Enter your password: (3 attempts left) ") == "secret":
197-
print("Welcome!")
198-
elif input("Enter your password: (2 attempts left) ") == "secret":
199-
print("Welcome!")
200-
elif input("Enter your password: (last attempt) ") == "secret":
201-
print("Welcome!")
202-
else:
203-
print("Access denied!")
90+
>>> ================================ RESTART ================================
91+
>>>
92+
Enter your username: foo
93+
Enter your password: biz
94+
Welcome foo!
95+
>>> ================================ RESTART ================================
96+
>>>
97+
Enter your username: bar
98+
Enter your password: baz
99+
Welcome bar!
100+
>>> ================================ RESTART ================================
101+
>>>
102+
Enter your username: spam
103+
Enter your password: eggs
104+
Wrong username.
105+
>>> ================================ RESTART ================================
106+
>>>
107+
Enter your username: foo
108+
Enter your password: lol
109+
Wrong password!
110+
>>>
204111
```
205112

206-
Example output:
207-
208-
>>> ================================ RESTART ================================
209-
>>>
210-
Enter your password: (3 attempts left) asdf
211-
Enter your password: (2 attempts left) asdf
212-
Enter your password: (last attempt) asdf
213-
Access denied!
214-
>>> ================================ RESTART ================================
215-
>>>
216-
Enter your password: (3 attempts left) asdf
217-
Enter your password: (2 attempts left) secret
218-
Welcome!
219-
>>>
220-
221113
***
222114

223115
You may use this tutorial freely at your own risk. See [LICENSE](LICENSE).

getting-started.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ In Python, these pieces of text starting with a `#` are known as
7979
**comments**. They don't effect the code in any way, but you can use
8080
them to explain what your code does.
8181

82-
### Using Python as a calculator
82+
## Using Python as a calculator
8383

8484
Maybe we could type mathematical statements?
8585

@@ -132,7 +132,7 @@ TypeError: unsupported operand type(s) for *: 'set' and 'int'
132132
>>>
133133
```
134134

135-
### More advanced math
135+
## More advanced math
136136

137137
I decided to include this in my tutorial because some people might be
138138
interested in this. Feel free to skip this if you're not interested.
@@ -190,7 +190,7 @@ Powers are calculated before `*` and `/`, but after `()`.
190190
>>>
191191
```
192192

193-
### Summary
193+
## Summary
194194

195195
- Errors don't matter.
196196
- You can enter any Python commands to the interactive `>>>` prompt, and

0 commit comments

Comments
 (0)