发布网友 发布时间:2022-04-24 02:05
共2个回答
热心网友 时间:2023-08-21 15:56
Q2:
源码:
word = input('Enter word: ')
reverse = word[::-1]
if word == reverse:
print('Palindrome? True')
else:
print('Palindrome? False')
运行结果:
>>> ================================ RESTART ================================
>>>
Enter word: racecar
Palindrome? True
>>> ================================ RESTART ================================
>>>
Enter word: research
Palindrome? False
>>>
Q3:
源码:
sentence = input('Type in a line of text: ')string = ''
for each_chr in sentence:
if each_chr.isalpha():
string += each_chr.lower()
print(string)
flag = string == string[::-1]
print('Palindrome?', flag)
运行一下:
>>> ================================ RESTART ================================
>>>
Type in a line of text: Doc, note: I dissent. A fast never prevents a fatness. I diet on cod.
docnoteidissentafastneverpreventsafatnessidietoncod
Palindrome? True
>>> ================================ RESTART ================================
>>>
Type in a line of text: Rise to vote sir!
risetovotesir
Palindrome? True
>>> ================================ RESTART ================================
>>>
Type in a line of text: A rose is a flower.
aroseisaflower
Palindrome? False
>>>
热心网友 时间:2023-08-21 15:57
课后题网上有答案的吧。