三个python问题!

发布网友 发布时间: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

课后题网上有答案的吧。

声明声明:本网页内容为用户发布,旨在传播知识,不代表本网认同其观点,若有侵权等问题请及时与本网联系,我们将在第一时间删除处理。E-MAIL:11247931@qq.com