我打算制作一个程序,询问用户一个句子,然后程序应检查句子是否有标点符号,如!"£$%^&*()[]#';,./<>?:@~}{_+-=
。
如果没有,则它要求用户在键入的句子中键入他们想要找到的单词。如果单词在句子中,那么它应该打印单词的位置。如果没有,则告诉用户该词不在句子中。
这是我的行为它并不是很好:
sentence = raw_input('Enter sentence:')
if char is sentence:
sentence = raw_input('Enter sentence:')
if char is not sentence:
word = raw_input('Enter word:')
while i is sentence:
print i
答案 0 :(得分:1)
你想要这样的东西:
import string
sentence = 'Test this sentence'
if not any([symbol in sentence for symbol in string.punctuation]):
# If no punctuations, continue ...
# Ask word and get word ... , for example:
word = 'this'
position = sentence.find(word) # Returns -1 if word not in sentence.
if position > -1:
print position