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
Copy file name to clipboardExpand all lines: answers.md
+27-7Lines changed: 27 additions & 7 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -2,7 +2,7 @@
2
2
3
3
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
+
## Chapter 1
6
6
7
7
1. 18996.20 €
8
8
@@ -53,7 +53,7 @@ These are answers for exercises in the chapters. In programming, there's always
53
53
>>>
54
54
```
55
55
56
-
### Chapter 3
56
+
## Chapter 3
57
57
58
58
1. Content of the file:
59
59
@@ -94,7 +94,7 @@ These are answers for exercises in the chapters. In programming, there's always
94
94
print(words *1000)
95
95
```
96
96
97
-
### Chapter 4
97
+
## Chapter 4
98
98
99
99
1. You can compare the word against an empty string (`""`or`''`). In this example, the password is"secret".
100
100
@@ -153,7 +153,8 @@ These are answers for exercises in the chapters. In programming, there's always
153
153
Wrong password!
154
154
>>>
155
155
156
-
3. This is a great chance to use a while loop. In this example, the correct password is"secret".
156
+
3. This is a great chance to use a while loop. In this example, the
157
+
correct password is"secret".
157
158
158
159
```py
159
160
running =True
@@ -166,15 +167,30 @@ These are answers for exercises in the chapters. In programming, there's always
166
167
print("Wrong password.")
167
168
```
168
169
169
-
Another alternative:
170
+
You can also use `break` to get out of a loop, and`whileTrue:` 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
+
whileTrue:
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:
170
185
171
186
```py
172
187
whileinput("Enter your password: ") !="secret":
173
188
print("Wrong password.")
174
189
print("Welcome!")
175
190
```
176
191
177
-
4. One way to do this is to put the inputs directly to `if`and`elif` lines. Again, the correct password is"secret".
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".
178
194
179
195
```py
180
196
ifinput("Enter your password: (3 attempts left) ") =="secret":
@@ -202,4 +218,8 @@ These are answers for exercises in the chapters. In programming, there's always
202
218
Welcome!
203
219
>>>
204
220
205
-
[Home](README.md)
221
+
***
222
+
223
+
You may use this tutorial freely at your own risk. See [LICENSE](LICENSE).
Copy file name to clipboardExpand all lines: using-functions.md
+3-3Lines changed: 3 additions & 3 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,6 +1,6 @@
1
1
# 3. Using functions and storing code in files
2
2
3
-
###Functions
3
+
## Functions
4
4
5
5
Now we know how to make Python show text.
6
6
@@ -103,7 +103,7 @@ Enter something: hello
103
103
>>>
104
104
```
105
105
106
-
###Storing code in files
106
+
## Storing code in files
107
107
108
108
Now it's time to write some code into a file for the first time. In IDLE, go to File at top left and select New File, or just press Ctrl+N.
109
109
@@ -136,7 +136,7 @@ After running the program you can check what the `word` variable contains in the
136
136
>>>
137
137
```
138
138
139
-
###Exercises
139
+
## Exercises
140
140
141
141
1. Write a program into a file that asks the user to write a word and then prints that word 1000 times. For example, if the user enters `hi` the program would reply `hihihihi...`.
142
142
2. Make it to print spaces between the words. It should be like `hi hi hi hi...`.
0 commit comments