3 # 4) Define a function that checks if a given string is a palindrome (reads the same forwards and backward) and prints True if the given string is a palindrome and False otherwise. Your program should be case insen for a given string an easy way to get its reversed form is using this shortcut the string[::-1] 5 def is_palindrome (s): 5 # 7 8 9 0 1 s=s.lower () return ==s[::-1] 2 str_1= "level" 3 str_2= "kennedy" I 34 str_3= "madam" 35 print (is_palindrome (str_1)) 36 print (is_palindrome (str_2)) 37 print (is_palindrome (str_3)) 38 # 5) Write a Python program that generates a random number between 1 and 100 and asks the user to guess it.

icon
Related questions
Question
100%
Something is wrong with this code. I get back “true false true” and idk why
The image shows a Python programming exercise focused on checking if a string is a palindrome. The script in the image is part of Lab 5 from a coding interface.

### Code Description:

1. **Function Definition:**
   - **Function Name:** `is_palindrome`
   - **Parameter:** `s` (a string)
   - **Purpose:** Checks if the input string is a palindrome (reads the same forwards and backward).
   - **Implementation Details:**
     - Converts the string `s` to lowercase using `s.lower()`.
     - Compares the string with its reverse (`s[::-1]`).
     - Returns `True` if they are the same, otherwise `False`.

2. **String Examples:**
   - `str_1 = "level"`
   - `str_2 = "kennedy"`
   - `str_3 = "madam"`

3. **Function Calls:**
   - `print(is_palindrome(str_1))` → Checks if "level" is a palindrome.
   - `print(is_palindrome(str_2))` → Checks if "kennedy" is a palindrome.
   - `print(is_palindrome(str_3))` → Checks if "madam" is a palindrome.

4. **Additional Commented Section:**
   - Mentions writing a Python program that generates a random number between 1 and 100, requiring the user to guess it while providing feedback on whether the guess is too high or too low.

### Output in the Console:

- User inputs a number `3`.
- The program checks the palindrome status of `str_2` which corresponds to the number input:
  - Displays: `string: kennedy`
  - Shows the count: `Count: 2`
  - Outputs:
    - `True` for "level"
    - `False` for "kennedy"
    - `True` for "madam"

### Graphs or Diagrams:
There are no graphs or diagrams in the image.

This script is useful for educational purposes to demonstrate string manipulation and basic logic in Python programming.
Transcribed Image Text:The image shows a Python programming exercise focused on checking if a string is a palindrome. The script in the image is part of Lab 5 from a coding interface. ### Code Description: 1. **Function Definition:** - **Function Name:** `is_palindrome` - **Parameter:** `s` (a string) - **Purpose:** Checks if the input string is a palindrome (reads the same forwards and backward). - **Implementation Details:** - Converts the string `s` to lowercase using `s.lower()`. - Compares the string with its reverse (`s[::-1]`). - Returns `True` if they are the same, otherwise `False`. 2. **String Examples:** - `str_1 = "level"` - `str_2 = "kennedy"` - `str_3 = "madam"` 3. **Function Calls:** - `print(is_palindrome(str_1))` → Checks if "level" is a palindrome. - `print(is_palindrome(str_2))` → Checks if "kennedy" is a palindrome. - `print(is_palindrome(str_3))` → Checks if "madam" is a palindrome. 4. **Additional Commented Section:** - Mentions writing a Python program that generates a random number between 1 and 100, requiring the user to guess it while providing feedback on whether the guess is too high or too low. ### Output in the Console: - User inputs a number `3`. - The program checks the palindrome status of `str_2` which corresponds to the number input: - Displays: `string: kennedy` - Shows the count: `Count: 2` - Outputs: - `True` for "level" - `False` for "kennedy" - `True` for "madam" ### Graphs or Diagrams: There are no graphs or diagrams in the image. This script is useful for educational purposes to demonstrate string manipulation and basic logic in Python programming.
Expert Solution
Step 1: Algorithm
  1. Start

  2. Define a function named is_palindrome that takes a string s as an input parameter.

  3. Inside the function: a. Convert the string s to lowercase using the lower() method and assign it back to s. This ensures the comparison is case insensitive. b. Check if the lowercase string s is equal to its reverse, which is s[::-1]. If they are equal, return True. Otherwise, return False.

  4. End of the function.

  5. Define three example strings: str_1, str_2, and str_3 with the values "level," "Kennedy," and "madam," respectively.

  6. Print the result of calling is_palindrome for each of the example strings:

    • is_palindrome(str_1) should print "True" because "level" is a palindrome.
    • is_palindrome(str_2) should print "False" because "Kennedy" is not a palindrome.
    • is_palindrome(str_3) should print "True" because "madam" is a palindrome.
  7. End of the program.

steps

Step by step

Solved in 4 steps with 2 images

Blurred answer
Similar questions