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.
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.
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.](/v2/_next/image?url=https%3A%2F%2Fcontent.bartleby.com%2Fqna-images%2Fquestion%2Fce303429-f349-4313-b1b6-39c40e965b05%2F8bb0ff94-ae83-4919-80ba-f7bf196814c4%2Fpwtbqx8_processed.jpeg&w=3840&q=75)
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
Start
Define a function named
is_palindrome
that takes a strings
as an input parameter.Inside the function: a. Convert the string
s
to lowercase using thelower()
method and assign it back tos
. This ensures the comparison is case insensitive. b. Check if the lowercase strings
is equal to its reverse, which iss[::-1]
. If they are equal, returnTrue
. Otherwise, returnFalse
.End of the function.
Define three example strings:
str_1
,str_2
, andstr_3
with the values "level," "Kennedy," and "madam," respectively.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.
End of the program.
Step by step
Solved in 4 steps with 2 images
