'Palindrome with two functions - Mooc exercise
I'm doing the Palindromes exercise in Section 4 (Definite Iteration) of Part 4 of the Intro to Programming on mooc.fi.
the exercise states:
Please write a function named palindromes, which takes a string argument and returns True if the string is a palindrome. Palindromes are words which are spelled exactly the same backwards and forwards.
Please also write a main function which asks the user to type in words until they type in a palindrome:
NB:, the main function should not be within an if __name__ == "__main__": block
I have written a code but I'm facing an issue with the code not getting executed:
def palindromes (word: str):
if word == word[::-1]:
return True
else:
return False
def main (word):
a= ""
while a != "Paliindrome":
word=input("Please type in a palindrome: ")
check = palindromes(word)
if word == "palindrome":
break
if check:
print(f"{word} is a palindrome!")
else:
print("that wasn't a palindrome")
Solution 1:[1]
def palindromes(word: str):
return word == word[::-1]
def main():
while True:
word = input("Please type in a palindrome: ")
if word == "exit":
break
if palindromes(word):
print(f"{word} is a palindrome!")
else:
print("that wasn't a palindrome")
main()
I think it is better:)
1.
word == word[::-1] is boolean and True or false and you dont need to check it,
2.
get your input and check it for break condition and dont set parameter for main function
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
