2

I am making a program which would ask the user to type in a word they want to know the position of in a list. However I am encountering a problem.

When I run my program providing the word "ask" as input, it doesn't print the sentence "Your number is in the num 'st position!". I expect it to be printed twice, as the word is in the list twice at exactly those positions.

Instead, the output is:

1
10
None

This is my code:

food = "ask not what your country can do for you ask what you can do for your country"
food2 = food.split()
print (food)

word = (input("Enter Word: "))

for (num, x) in enumerate(list(food2)):
    if word == x:
        print (num + 1)
        if num == "1" and "10" :
            print ("Your number is in the ",num,"st position!")
moooeeeep
  • 31,622
  • 22
  • 98
  • 187
T.Cho
  • 27
  • 1
  • 7

2 Answers2

3

list(food2) is not necessary

if num == "1" and "10" : you are checking a number with an integer, and then seeing if "10" is true, which is always true as it always exists. Why do you need this statement?

food = "ask not what your country can do for you ask what you can do for your country"
food2 = food.split()
print(food)

word = input("Enter Word: ")

for (num, x) in enumerate(food2):
    if word == x:
        print ("Your word is in position ",num+1,"!")
Ignacy Debicki
  • 437
  • 4
  • 18
  • When I run the program, I am given the option to choose which word I want to know the position of. I am also testing the program so I am using the word 'ask' to check if it works or not. 'Ask' appears in the 1st and 10th positions. – T.Cho Nov 16 '15 at 18:05
  • the program will let the user choose words from the 'food' variable. – T.Cho Nov 16 '15 at 18:06
  • This code should work for any word. You should not be using if statements to test if the code works. You should make a test case, such as "Put in the word ask", define your expected output "Your word is in position 1! \n Your word is in position 10!". Finally, check if your prediction matches up with your actual output. This way, you do not need to modify the code to test it. – Ignacy Debicki Nov 16 '15 at 18:07
  • I have done what was said, it is now working but the word 'None' returned aswell. Is there anyway to remove this? – T.Cho Nov 16 '15 at 18:30
  • The code I posted does not return None at the end of my program. What version of python are you running and how are you running the program? (CLI, IDLE etc.) I'm using python 3.4.3 with IDLE on mac and i'm not getting the 'None' output – Ignacy Debicki Nov 16 '15 at 18:44
  • I have realized the mistake I had made, this problem is fixed. Another problem I am facing is that I have used the code you have posted, but I want to give the numbers ordinal suffixes, and I have noticed that the code you had posted just states ' your code is in position x'. Anyway to resolve this? Also I am on Python 3.5 with IDLE on a PC. – T.Cho Nov 16 '15 at 19:08
  • Look here: http://codereview.stackexchange.com/questions/41298/producing-ordinal-numbers The guy's suggested solution at the bottom of the selected answer is best – Ignacy Debicki Nov 16 '15 at 21:27
  • So you would add that function in, and then using ordinal(num+1) will return the ordinal string of the number – Ignacy Debicki Nov 16 '15 at 21:32
-1

Use raw_input instead of input. Also, "10" will always be true in Python, so checking if it is is pointless.