Skip to content

Commit d23053a

Browse files
committed
again, better exercises
1 parent d97dff1 commit d23053a

File tree

1 file changed

+32
-37
lines changed

1 file changed

+32
-37
lines changed

if.md

Lines changed: 32 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ SyntaxError: invalid syntax
6666
>>>
6767
```
6868

69-
Functions like `print` need `()` after their name to work. But `if` is
69+
**Functions** like `print` need `()` after their name to work. But `if` is
7070
a keyword, not a function, so it doesn't need `()`. Python has separate
7171
functions and keywords because it's possible to create custom functions,
7272
but it's not possible to create custom keywords. That's why keywords are
@@ -286,52 +286,47 @@ else:
286286

287287
## Exercises
288288

289-
1. Write a program into a file that asks the user to write a word and
289+
1. This program contains several problems. Copy-paste it to a file,
290+
then try to run it, fix the errors you got, try to run it again and
291+
keep going until it works.
292+
293+
```py
294+
print(Hello!)
295+
something == input('Enter something: )
296+
print('You entered:' something)
297+
```
298+
299+
2. Fix this program the same way:
300+
301+
```py
302+
print('Hello!')
303+
something = input("Enter something: ")
304+
if something = 'hello':
305+
print("Hello for you too!")
306+
elif something = 'hi'
307+
print('Hi there!')
308+
else:
309+
print("I don't know what," something, "means.")
310+
```
311+
312+
3. Write a program into a file that asks the user to write a word and
290313
then prints that word 1000 times. For example, if the user enters
291-
`hi` the program would reply `hihihihi...`.
314+
`hi` the program would reply `hihihihi` and so on.
292315

293-
2. Add spaces between the words, so the output is like `hi hi hi ...`.
316+
4. Add spaces between the words, so the output is like `hi hi hi` and
317+
so on.
294318

295-
3. Make something that asks the user to enter two words, and prints
319+
5. Make something that asks the user to enter two words, and prints
296320
1000 of each with spaces in between. For example, if the user
297321
enters `hello` and `hi` the program would print
298-
`hello hi hello hi hello hi...`.
322+
`hello hi hello hi hello hi` and so on.
299323

300-
4. Make a program that asks for a password and prints `Welcome!`,
324+
6. Make a program that asks for a password and prints `Welcome!`,
301325
`Access denied` or `You didn't enter anything` depending on whether
302326
the user entered the correct password, a wrong password, or nothing
303327
at all by pressing Enter without typing anything.
304328

305-
5. Make a program that asks for username and password and checks them.
306-
Make users "foo" and "bar" with passwords "biz" and "baz".
307-
308-
Example output:
309-
310-
```
311-
>>> ================================ RESTART ================================
312-
>>>
313-
Enter your username: foo
314-
Enter your password: biz
315-
Welcome foo!
316-
>>> ================================ RESTART ================================
317-
>>>
318-
Enter your username: bar
319-
Enter your password: baz
320-
Welcome bar!
321-
>>> ================================ RESTART ================================
322-
>>>
323-
Enter your username: spam
324-
Enter your password: eggs
325-
Wrong username.
326-
>>> ================================ RESTART ================================
327-
>>>
328-
Enter your username: foo
329-
Enter your password: lol
330-
Wrong password!
331-
>>>
332-
```
333-
334-
The answers are [here](answers.md).
329+
The answers are [here](answers.md#if-else-and-elif).
335330

336331
***
337332

0 commit comments

Comments
 (0)