0
sentence='ASK NOT WHAT YOUR COUNTRY CAN DO FOR YOU ASK WHAT YOU CAN DO FOR YOUR COUNTRY'
word=[]
pos=0
choice=''
while choice!='q':
    print(sentence)
    word=sentence.split(' ')
    choice=input('what word do you want to find').upper()
    for pos in range(len(word)):
        if choice==word[pos]:
            print('The word '  + str(choice)+  ' occurs in the ' + str(pos +1) + ' th position ')
    if choice not in word:
        print("not valid word")

So, I have got this code that prints the words location in my array but lets say the word is in the zero position e.g Ask I want it to come up with the word ASK occurs in the 1st position instead of the word ASK occurs in the th position, and so on e.g nd,rd and th. Any help would be appreciated!

joe
  • 5
  • 4
  • What about repeating words? – Padraic Cunningham May 10 '16 at 09:18
  • yes so it would say the word ask occurs in the 1st position and the 10th position – joe May 10 '16 at 09:20
  • Use a dict and store the indexes for each word in lists as values then just do a lookup in the dict – Padraic Cunningham May 10 '16 at 09:21
  • im quite new to python and im not quite sure how I would do that. Sorry about the inconvenience. – joe May 10 '16 at 09:23
  • Isn't [this](http://stackoverflow.com/questions/3221891/how-can-i-find-the-first-occurrence-of-a-sub-string-in-a-python-string) what you are looking for (and then create a substring from the word on that position)? – Jos May 10 '16 at 09:25
  • no, because I already can do that. Im trying to find out how to add number suffixes to my output. – joe May 10 '16 at 09:26

3 Answers3

0

You could use the humanize package, specifically the function humanize.number.ordinal() does exactly what you want:

>>> from humanize import number
>>> for i in range(30):
...     print(number.ordinal(i))
1st
2nd
3rd
4th
5th
6th
7th
8th
9th
10th
11th
12th
13th
14th
15th
16th
17th
18th
19th
20th
21st
22nd
23rd
24th
25th
26th
27th
28th
29th

For your code you could change the print() to this:

print('The word '  + str(choice)+  ' occurs in the ' + number.ordinal(pos+1) + ' position ')

Or better, use str.format():

print('The word {} occurs in the {} position'.format(choice, number.ordinal(pos+1)))
mhawke
  • 84,695
  • 9
  • 117
  • 138
0

Old post, but posting in case others find this post like I did when searching. This is what I wrote up, this returns a string containing the source number and its appropriate prefix.

Example: ordinal(100) will return '100th'.

def ordinal(n):
    if str(n)[-1] == '1':
        return str(n) + 'st'
    elif str(n)[-1] == '2':
        return str(n) + 'nd'
    elif str(n)[-1] == '3':
        return str(n) + 'rd'
    else:
        return str(n) + 'th'
-1

You could use a function something like this:

def ordinal(n):
    if n == 1:
        return "first"
    elif n == 2:
        return "second"
    elif n == 3:
        return "third"
    else: 
        return str(n)+"th"

incorporating it into your code is left as an exercise :-)

Mr Shark
  • 26,068
  • 5
  • 29
  • 37