Lord MagnusMardak
Lord MagnusMardak

Reputation: 41

Print and Find The Position Of A Word And Words In A List

I am set to make a program that ask the user for a sentence then the program should check if the sentence has punctuation such as !"£$%^&*()[]#';,./<>?:@~}{_+-=.

If it does not then it asks the user to type the word they want to find in the typed sentence. If the word is in the sentence then it should print the position of the word. If not then tell the user the word is not in the sentence.

Here is my go It's not pretty good:

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

Upvotes: 0

Views: 1019

Answers (1)

Rutger Woolthuis
Rutger Woolthuis

Reputation: 26

You want something like this:

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

Upvotes: 1

Related Questions