Skip to content

Commit 2a9cd88

Browse files
committed
and the answers
1 parent d23053a commit 2a9cd88

File tree

1 file changed

+28
-26
lines changed

1 file changed

+28
-26
lines changed

answers.md

Lines changed: 28 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,43 @@
11
# Answers
22

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.
46

57
## ThinkPython: The way of the program
68

79
1. With +, the strings get added together, and with * we get an error.
810
2. With + we get an error, and with * the string is repeated multiple times.
911
3. Python calculates the result and echoes it.
1012

11-
## Using if, else and elif
13+
## If, else and elif
1214

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`.
1434

1535
```py
1636
word = input("Enter a word: ")
1737
print(word * 1000)
1838
```
1939

20-
2. Add a space to the word before printing.
40+
4. We can add a space to the word before we print it.
2141

2242
```py
2343
word = input("Enter a word: ")
@@ -43,7 +63,7 @@ These are answers for exercises in the chapters. In programming, there's always
4363
print(yes_space * 999 + no_space)
4464
```
4565

46-
3. Like this:
66+
5. Like this:
4767

4868
```py
4969
first = input("Enter a word: ")
@@ -52,7 +72,7 @@ These are answers for exercises in the chapters. In programming, there's always
5272
print(words * 1000)
5373
```
5474

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
5676
check if it's empty. In this example, the password is "secret".
5777

5878
```py
@@ -66,26 +86,8 @@ These are answers for exercises in the chapters. In programming, there's always
6686
print("Access denied.")
6787
```
6888

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 is not 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.
8991

9092
## Loops
9193

0 commit comments

Comments
 (0)