简体   繁体   中英

If key in dictionary returning false when key exists

Python 3.2

This might be really stupid but:

items = {'1': 'ACK', '2': 'RELQ', '3': 'COM'}
choice = input('Choose an option:\n1.ACK\n2.RELQ\n3.COM\n')
print(choice)
if choice in items:
    print(choice)
    option = items[choice]
else:
    print('Input not recognized')

If you type 1 it keeps returning

1
Input not recognized

The choice in items is returning a false?

This should be really easy but I just cant see what it is.

UPDATE:

print(type(choice))` returns str

print(len(choice)) returns 2

print(repr(choice)) returns '1\r'
print(choice[0]) returns 1

The input was receiving a \\r newline character with the input()

Probably it grabs the newline character, so in order for your code to work you should trim the choices variable. Alternatively if your dict keys would be ints instead of strings, you could just perform casting operation.

EDIT: your last comment proves my point because of the [49, 13] numbers - 13 is the ascii code for carriage return.

Just add:

choice = choice.strip()

Before the if choice in items:

You are seeing a bug in Python 3.2.0, fixed in 3.2.1 : input() should not give you any trailing carriage return/newline character. I encourage you to upgrade Python, if you can.

I you are stuck with Python 3.2.0, you must either remove the trailing \\r character:

import os
choice = input(…).rstrip(os.linesep)  # Robust (no assumption)

or use integers throughout:

items = {1: …}
choice = int(input(…))

This second choice is more natural, I would say, in your case, but it does not work if you also want to add letters as choices.

PS: Łukasz R.'s answer is good, too: it gives the added bonus of trimming whitespaces (trailing spaces,…).

You are grabbing extra white-space characters (either \\n or \\r). After getting input simply use:

choice = choice.strip()

As a result your string would be trimmed from both sides. If starting whitespace characters matters use:

choice = choice.rstrip()

Another way of solving this is by converting the input return value into string, since the keys in your items dictionary are strings, like this:

items = {'1': 'ACK', '2': 'RELQ', '3': 'COM'}
choice = input('Choose an option:\n1.ACK\n2.RELQ\n3.COM\n')
choice = str(choice)
print(choice)
if choice in items:
    print(choice)
    option = items[choice]
else:
    print('Input not recognized')

You can also make life easier for you, if you don't care about the type of your dictionary' Keys (whether string, integer...etc), you can simply define them as integers rather than strings, like this:

items = {1: 'ACK', 2: 'RELQ', 3: 'COM'}
choice = input('Choose an option:\n1.ACK\n2.RELQ\n3.COM\n')
print(choice)
if choice in items:
    print(choice)
    option = items[choice]
else:
    print('Input not recognized')

Like this you don't need to string any extra character and you save yourself extra coding.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM