我正在完成一个接受sys参数(字符串)并检查它是否是回文的程序。如果我只有一个单词字符串,我当前的代码就可以工作。但是,我需要确保程序将检查每个单词,而不是任何非字母数字字符的因素。如果其中任何一个单词是回文,那么它会一次性打印它是回文
思维过程:
我的想法是将每个单词分成一个列表,让列表迭代以查看是否满足条件,然后只打印一次。如果我有多个单词,它会打印出来,而不是回文。目前,如果我提供一个包含多个单词的字符串,它只会多次打印它不是回文:即“racecar,racecar”
如有任何建议,将不胜感激
这是到目前为止我的代码
def palindrome():
string = sys.argv[1].lower()
remove_punc = string.strip('~`!@$%^&*()_-+={}[]|\:;<,>.?/')
converted_string = remove_punc.split(' ')
for i in converted_string:
if converted_string == converted_string[::-1]:
print('It\'s a palindrome!')
break
else:
print('It\'s not a palindrome!')
palindrome()
当您输入超过1个单词(前面或后面有空格的东西)时, 如果您想获得每个单词,则需要获取除程序本身之外的所有参数。然后从非alpha字符中清除它们,然后可以测试它们
测试:
我很感激这些意见书,这是我工作的主要部分
你是说这个吗
Update
没有正则表达式:
要使用args,请执行以下操作:
Update 2
如果要同时检查多个回文,请执行以下操作:
相关问题 更多 >
编程相关推荐