diff --git a/LICENSE b/LICENSE index e9c652f..93baa59 100644 --- a/LICENSE +++ b/LICENSE @@ -1,7 +1,7 @@ The instructions and text in this tutorial (the "software") are licensed under the zlib License. - (C) 2016-2018 Akuli + (C) 2016-2021 Akuli This software is provided 'as-is', without any express or implied warranty. In no event will the authors be held liable for any damages diff --git a/basics/answers.md b/basics/answers.md index de739c3..f2fcdf1 100644 --- a/basics/answers.md +++ b/basics/answers.md @@ -92,7 +92,7 @@ isn't exactly like mine but it works just fine it's ok, and you can print("Access denied.") ``` - Again, this is not a good way to ask a real password from the user. + Again, this is not a good way to ask a real password from the user. ## Handy stuff: Strings @@ -156,7 +156,15 @@ isn't exactly like mine but it works just fine it's ok, and you can print(message, "!!!") print(message, "!!!") ``` - +3. In the code below, `palindrome_input[::-1]` is the string `palindrome_input` reversed. + For example, if `palindrome_input` is `"hello"`, then `palindrome_input[::-1]` is `"olleh"`. + ```python + palindrome_input=input("Type the number to check:") + if palindrome_input == palindrome_input[::-1]: + print("This string is a palindrome") + else: + print("This string is not a palindrome") + ``` ## Lists and tuples 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 print(converted_numbers) ``` +5. ``` python + row_count = int(input("Type the number of rows needed:")) + for column_count in range(1, row_count+1): + # Print numbers from 1 to column_count + for number in range(1, column_count+1): + print(number, end=" ") + print() # creates a new line for the next row + ``` + If the user enters 5, we want to do a row with 1 column, then 2 columns, and so on until 5 columns. + That would be `for column_count in range(1, 6)`, because the end of the range is excluded. + In general, we need to specify `row_count + 1` so that it actually ends at `row_count`. + The second loop is similar. + + Usually `print(number)` puts a newline character at the end of the line, so that the next print goes to the next line. + To get all numbers on the same line, we use a space instead of a newline character, + but we still need `print()` to add a newline character once we have printed the entire row. + + + +6. ```python + row_count=int(input("Type the number of rows needed:")) + + for line_number in range(1, row_count + 1): + for number in range(line_number, row_count+1): + print(number, end=' ') + print() + ``` + Just like in the previous exercise, if the user enters 5, the first `for` loop gives the line numbers `1, 2, 3, 4, 5`.
+ 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)`. + ## Trey Hunner: zip and enumerate 1. Read some lines with `input` into a list and then enumerate it. diff --git a/basics/handy-stuff-strings.md b/basics/handy-stuff-strings.md index be89c55..c8458ce 100644 --- a/basics/handy-stuff-strings.md +++ b/basics/handy-stuff-strings.md @@ -424,6 +424,8 @@ ValueError: could not convert string to float: 'hello' print(message, "!!!") print(message, "!!!") ``` +3. Make a program to take the input from the user and check if it is a palindrome.
+ (Hint: A string is a palindrome if it is the same when reversed. Google how to reverse a string.) The answers are [here](answers.md#handy-stuff-strings). diff --git a/basics/if.md b/basics/if.md index a1fed8e..9247ed6 100644 --- a/basics/if.md +++ b/basics/if.md @@ -312,6 +312,7 @@ you are asked to "fix code", feel free to add missing blank lines. the user entered the correct password, a wrong password, or nothing at all by pressing Enter without typing anything. + The answers are [here](answers.md#if-else-and-elif). *** diff --git a/basics/loops.md b/basics/loops.md index 4abb172..4e69c60 100644 --- a/basics/loops.md +++ b/basics/loops.md @@ -470,7 +470,25 @@ while True: number = int(number) print(numbers) ``` - +5. Make a program that prints a pyramid like shown below. Ask the user to type the number of rows needed. + ``` + OUTPUT for 5 rows + 1 + 1 2 + 1 2 3 + 1 2 3 4 + 1 2 3 4 5 + ``` + +6. Make a program to get a pyramid like shown below where user can type the number of rows needed. + ``` + OUTPUT for 5 rows + 1 2 3 4 5 + 2 3 4 5 + 3 4 5 + 4 5 + 5 + ``` The answers are [here](answers.md#loops). ***