【发布时间】:2013-12-05 06:58:31
【问题描述】:
def char_check(x,y):
if (str(x) in y or x.find(y) > -1) or (str(y) in x or y.find(x) > -1):
return True
else:
return False
print "You will enter two words that you think use some of the same letters."
x = raw_input('Enter one of the words: ')
y = raw_input('Enter the other word: ')
print char_check(x,y)
我要做的是输入两个字符串,例如 str(x) 的 "terrible" 和 str(y) 的 "bile" 并返回 "True" 因为字符 'b'、'i'、' l' 和 'e' 由两个字符串共享。
我是新手,正在努力学习,但我似乎无法自己解决这个问题。谢谢大家。
【问题讨论】:
-
您想要任何字符还是所有字符?例如
terrible和boo的输出应该是什么? -
感谢您的回复。对于 'terrible' 和 'boo' 函数应该返回 True 因为 'b'
-
这是一些教程中的练习吗?即使没有集合,您也应该能够使用 for 循环和
in编写快速解决方案。 -
@RemcoGerlich :是的,这是来自练习。我正在审核的课程还没有介绍套路。我如何只使用 for 循环和“in”来做到这一点?
-
循环遍历其中一个字符串中的所有字符。如果字符在另一个字符串中,欢呼,返回 True。如果我们完成但尚未返回,则返回 False。
标签: python string python-2.7 user-defined-functions