You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
4
4
5
-
## Chapter 1
5
+
## ThinkPython: The way of the program
6
6
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.
8
10
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
59
12
13
+
1.
60
14
```py
61
15
word =input("Enter a word: ")
62
16
print(word *1000)
63
17
```
64
18
65
-
2. Content of the file:
66
-
19
+
2.
67
20
```py
68
21
word =input("Enter a word: ")
69
22
word +=""
70
23
print(word *1000)
71
24
```
72
25
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:
74
27
75
28
```py
76
29
word =input("Enter a word: ") +""
77
30
print(word *1000)
78
31
```
79
32
80
-
Of course, there are 999 spaces between 1000 words and this will print1000 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
+
print1000 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:
81
37
82
38
```py
83
39
no_space =input("Enter a word: ")
84
40
yes_space = no_space +""
85
41
print(yes_space *999+ no_space)
86
42
```
87
43
88
-
3. Like this:
89
-
44
+
3.
90
45
```py
91
46
first =input("Enter a word: ")
92
47
second =input("Enter another word: ")
93
48
words = first +""+ second +""
94
49
print(words *1000)
95
50
```
96
51
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".
100
54
101
55
```py
102
56
word =input("Enter your password: ")
@@ -109,7 +63,8 @@ These are answers for exercises in the chapters. In programming, there's always
109
63
print("Access denied.")
110
64
```
111
65
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.
113
68
114
69
```py
115
70
username =input("Enter your username: ")
@@ -131,93 +86,30 @@ These are answers for exercises in the chapters. In programming, there's always
0 commit comments