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
+28-26Lines changed: 28 additions & 26 deletions
Original file line number
Diff line number
Diff line change
@@ -1,23 +1,43 @@
1
1
# Answers
2
2
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.
3
+
These are my answers for exercises in the chapters. If your solution
4
+
isn't exactly like mine but it works just fine it's ok, and you can
5
+
[ask me](contact-me.md) why I didn't do it like you did it.
4
6
5
7
## ThinkPython: The way of the program
6
8
7
9
1. With +, the strings get added together, and with * we get an error.
8
10
2. With + we get an error, and with * the string is repeated multiple times.
9
11
3. Python calculates the result and echoes it.
10
12
11
-
## Using if, else and elif
13
+
## If, else and elif
12
14
13
-
1. Just ask the word and print word * 1000.
15
+
1. Problems and solutions:
16
+
17
+
- The first line should be `print("Hello!")` or `print('Hello!')`,
18
+
not `print(Hello!)`. `Hello!` is a piece of text, so we need to
19
+
tell Python that it's a string by putting quotes around it.
20
+
- The second line will create an error message that says that
21
+
there's no variable called `something`. But we are trying to
22
+
create it here, so we need `=` instead of `==`. `=` is
23
+
assigning, `==` is comparing.
24
+
- The last line should have a comma between the arguments, like
25
+
`print('You entered:', something)`.
26
+
27
+
2. The broken code has mostly the same issues as exercise 1. On the
28
+
last line the comma is on the wrong side. **`"bla bla,"` is a
29
+
string that contains a comma, but `"bla bla",` is a string and a
30
+
separate comma.** In this exercise, the last line should be
31
+
`print("I don't know what", something, "means.")`
32
+
33
+
3. We can simply ask the word with input and print `word * 1000`.
14
34
15
35
```py
16
36
word =input("Enter a word: ")
17
37
print(word *1000)
18
38
```
19
39
20
-
2. Add a space to the word before printing.
40
+
4. We can add a space to the word before we print it.
21
41
22
42
```py
23
43
word =input("Enter a word: ")
@@ -43,7 +63,7 @@ These are answers for exercises in the chapters. In programming, there's always
43
63
print(yes_space *999+ no_space)
44
64
```
45
65
46
-
3. Like this:
66
+
5. Like this:
47
67
48
68
```py
49
69
first =input("Enter a word: ")
@@ -52,7 +72,7 @@ These are answers for exercises in the chapters. In programming, there's always
52
72
print(words *1000)
53
73
```
54
74
55
-
4. You can compare the word against an empty string (`""`or`''`) to
75
+
6. We can compare the word against an empty string (`""`or`''`) to
56
76
check if it's empty. In this example, the password is "secret".
57
77
58
78
```py
@@ -66,26 +86,8 @@ These are answers for exercises in the chapters. In programming, there's always
66
86
print("Access denied.")
67
87
```
68
88
69
-
5. Simply check the username first, then the password in indented
70
-
blocks of code.
71
-
72
-
```py
73
-
username =input("Enter your username: ")
74
-
password =input("Enter your password: ")
75
-
76
-
if username =="foo":
77
-
if password =="biz":
78
-
print("Welcome foo!")
79
-
else:
80
-
print("Wrong password!")
81
-
elif username =="bar":
82
-
if password =="baz":
83
-
print("Welcome bar!")
84
-
else:
85
-
print("Wrong password!")
86
-
else:
87
-
print("Wrong username.")
88
-
```
89
+
This isnot a good way to ask a password from the user because the
90
+
password isn't hidden in any way, but this is just an example.
0 commit comments