Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 18c7cea

Browse files
New Excercises (#30)
1 parent 6af5196 commit 18c7cea

File tree

5 files changed

+63
-4
lines changed

5 files changed

+63
-4
lines changed

LICENSE

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
The instructions and text in this tutorial (the "software") are licensed
22
under the zlib License.
33

4-
(C) 2016-2018 Akuli
4+
(C) 2016-2021 Akuli
55

66
This software is provided 'as-is', without any express or implied
77
warranty. In no event will the authors be held liable for any damages

basics/answers.md

+40-2
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ isn't exactly like mine but it works just fine it's ok, and you can
9292
print("Access denied.")
9393
```
9494

95-
Again, this is not a good way to ask a real password from the user.
95+
Again, this is not a good way to ask a real password from the user.
9696

9797
## Handy stuff: Strings
9898

@@ -156,7 +156,15 @@ isn't exactly like mine but it works just fine it's ok, and you can
156156
print(message, "!!!")
157157
print(message, "!!!")
158158
```
159-
159+
3. In the code below, `palindrome_input[::-1]` is the string `palindrome_input` reversed.
160+
For example, if `palindrome_input` is `"hello"`, then `palindrome_input[::-1]` is `"olleh"`.
161+
```python
162+
palindrome_input=input("Type the number to check:")
163+
if palindrome_input == palindrome_input[::-1]:
164+
print("This string is a palindrome")
165+
else:
166+
print("This string is not a palindrome")
167+
```
160168
## Lists and tuples
161169

162170
1. Look carefully. The `namelist` is written in `()` instead of `[]`,
@@ -296,6 +304,36 @@ isn't exactly like mine but it works just fine it's ok, and you can
296304
print(converted_numbers)
297305
```
298306
307+
5. ``` python
308+
row_count = int(input("Type the number of rows needed:"))
309+
for column_count in range(1, row_count+1):
310+
# Print numbers from 1 to column_count
311+
for number in range(1, column_count+1):
312+
print(number, end=" ")
313+
print() # creates a new line for the next row
314+
```
315+
If the user enters 5, we want to do a row with 1 column, then 2 columns, and so on until 5 columns.
316+
That would be `for column_count in range(1, 6)`, because the end of the range is excluded.
317+
In general, we need to specify `row_count + 1` so that it actually ends at `row_count`.
318+
The second loop is similar.
319+
320+
Usually `print(number)` puts a newline character at the end of the line, so that the next print goes to the next line.
321+
To get all numbers on the same line, we use a space instead of a newline character,
322+
but we still need `print()` to add a newline character once we have printed the entire row.
323+
324+
325+
326+
6. ```python
327+
row_count=int(input("Type the number of rows needed:"))
328+
329+
for line_number in range(1, row_count + 1):
330+
for number in range(line_number, row_count+1):
331+
print(number, end=' ')
332+
print()
333+
```
334+
Just like in the previous exercise, if the user enters 5, the first `for` loop gives the line numbers `1, 2, 3, 4, 5`.<br>
335+
For example, on line 2, we should print numbers from 2 to 5, as in `range(2, 6)`, or in general, `range(line_number, row_count+1)`.
336+
299337
## Trey Hunner: zip and enumerate
300338
301339
1. Read some lines with `input` into a list and then enumerate it.

basics/handy-stuff-strings.md

+2
Original file line numberDiff line numberDiff line change
@@ -424,6 +424,8 @@ ValueError: could not convert string to float: 'hello'
424424
print(message, "!!!")
425425
print(message, "!!!")
426426
```
427+
3. Make a program to take the input from the user and check if it is a palindrome.<br>
428+
(Hint: A string is a palindrome if it is the same when reversed. Google how to reverse a string.)
427429

428430
The answers are [here](answers.md#handy-stuff-strings).
429431

basics/if.md

+1
Original file line numberDiff line numberDiff line change
@@ -312,6 +312,7 @@ you are asked to "fix code", feel free to add missing blank lines.
312312
the user entered the correct password, a wrong password, or nothing
313313
at all by pressing Enter without typing anything.
314314

315+
315316
The answers are [here](answers.md#if-else-and-elif).
316317

317318
***

basics/loops.md

+19-1
Original file line numberDiff line numberDiff line change
@@ -470,7 +470,25 @@ while True:
470470
number = int(number)
471471
print(numbers)
472472
```
473-
473+
5. Make a program that prints a pyramid like shown below. Ask the user to type the number of rows needed.
474+
```
475+
OUTPUT for 5 rows
476+
1
477+
1 2
478+
1 2 3
479+
1 2 3 4
480+
1 2 3 4 5
481+
```
482+
483+
6. Make a program to get a pyramid like shown below where user can type the number of rows needed.
484+
```
485+
OUTPUT for 5 rows
486+
1 2 3 4 5
487+
2 3 4 5
488+
3 4 5
489+
4 5
490+
5
491+
```
474492
The answers are [here](answers.md#loops).
475493
476494
***

0 commit comments

Comments
 (0)