Path: csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail From: jacob Kruger Newsgroups: comp.lang.python Subject: Re: Help Date: Sun, 28 Feb 2016 22:50:47 +0200 Lines: 34 Message-ID: References: <6cdcd063-1c22-4f08-900c-f3260a543018@googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain; charset=windows-1252; format=flowed Content-Transfer-Encoding: 7bit X-Trace: news.uni-berlin.de VWRleu37QuzFs6QCFFEM0Q+59lyiqjO9dXJpRIeL2R8w== Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.031 X-Spam-Evidence: '*H*': 0.94; '*S*': 0.00; 'typed': 0.09; 'subject:Help': 0.10; 'index': 0.13; 'jacob': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'wrote:': 0.16; 'string,': 0.18; 'solution.': 0.18; 'thanks.': 0.18; 'gather': 0.22; 'strip': 0.22; 'appears': 0.23; 'split': 0.23; 'header:In-Reply-To:1': 0.24; 'header:User-Agent:1': 0.26; 'object,': 0.27; 'function': 0.28; 'asks': 0.29; 'sentence': 0.29; 'spaces': 0.29; 'allows': 0.30; 'code': 0.30; 'e.g.': 0.30; 'common': 0.33; '"")': 0.33; 'loading': 0.33; 'list': 0.34; 'something': 0.35; 'item': 0.35; 'but': 0.36; 'list,': 0.36; 'lines': 0.36; 'to:addr:python-list': 0.36; 'pm,': 0.36; 'subject:: ': 0.37; 'list.': 0.37; 'skype:': 0.38; 'along': 0.39; 'to:addr:python.org': 0.40; 'space': 0.40; 'default': 0.61; 'email addr:gmail.com': 0.62; 'charset:windows-1252': 0.62; 'within': 0.64; 'here': 0.66; 'wish': 0.71 Comment: DKIM? See http://www.dkim.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=blindza.co.za; s=dkim-shared; x=1457297844; h=Comment: DomainKey-Signature:Received:Received:Subject:To:References:From: Message-ID:Date:User-Agent:MIME-Version:In-Reply-To:Content-Type: Content-Transfer-Encoding; bh=f0WUNwlTKqyFn1Oj2qEYPLZqPcD6tit91i UpZ6EWzko=; b=lwrVicichr95iHxzNt3m0KBjb984GNQcG9GjxGrpB0PFsVolCC Fc/bMwKHGUwo/0I6gU5w4UNA2MhqXPdRlLJ948ZTYBLGEcVAJLm7QE4IQ3sjOSLQ LUCCyxQZZC0AeEQkllxXrE0OR8iN8kI0YYwSmoh4Gu3ZZQnEKC9553rK8= Comment: DomainKeys? See http://antispam.yahoo.com/domainkeys DomainKey-Signature: a=rsa-sha1; q=dns; c=nofws; s=dkim-shared; d=blindza.co.za; h=Received:Received:X-Originating-IP:X-AMS-Scanned:Subject:To:References:From:Message-ID:Date:User-Agent:MIME-Version:In-Reply-To:Content-Type:Content-Transfer-Encoding:X-Brightmail-Tracker:X-AMS-SPAM-PROBABILITY; b=vlh6gvV8URNdgR5oEkdT0XJWEkErPc4Z54TIr9s1mhILecWAvc8fNyuxWZcjdC fnA3EGomjrtciNjUoVgVPlxzEDh4A3sPYFK4OYjsMDChGhQWTP/Ce9hSplp0dYbV JXCD44jjKyXdcQx6ZvyOgvAlMW76cMSU9Tdoes8WEwlfE=; X-Originating-IP: 196.207.44.126 X-AMS-Scanned: mail from: 100.80.233.225 to 98.130.1.137 on Feb Sun 22:50:47 2016 User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; rv:38.0) Gecko/20100101 Thunderbird/38.6.0 In-Reply-To: <6cdcd063-1c22-4f08-900c-f3260a543018@googlegroups.com> X-Brightmail-Tracker: AAAAAA== X-AMS-SPAM-PROBABILITY: 0 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.21rc2 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Xref: csiph.com comp.lang.python:103661 On 2016-02-28 9:58 PM, tomwilliamson115@gmail.com wrote: > I need to write a program that allows the user to enter a sentence then asks them which word they wish to find- and then tell them the position that word was within the sentence. > > E.g. Please help with this code > Then they enter help it would return that it appears in the 2nd position in the sentence. > >>From what I gather it appears to be a list function but I am struggling to come up with a solution. > > Thanks. > Something along lines of loading the sentence into a string, using the str.split() function to split it into a list object, then cycling through to strip all the spaces and/or punctuation out of the elements/items, and then the list.index() function can return the index of a word in the list, which is the zero-based position of the item in that list. #something along lines of s = input("enter sentence") s.replace(",", "") #do this for all common punctuation characters l = s.split(" ") #space is actually default s2 = input("enter word") i = l.index(s2) print("Your word is at position " + str(i+1) + " in the sentence") #end code That's just typed here in message - HTH HTH Jacob Kruger Blind Biker Skype: BlindZA "Roger Wilco wants to welcome you...to the space janitor's closet..."