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
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