beary
beary

Reputation: 33

Using Tuples as Keys in Python

I'm very new to programming so forgive me if anything doesn't make sense or if I word things incorrectly. I have a question about using tuples as dictionary keys.

First, I have the user input a number,

num = input("Enter a number here: ")

Then, I turn this number value into a tuple:

numTup = tuple(num)

Next, I create a dictionary connecting numerical keys to word values:

numWords = {'1': "One", '2': "Two", '3': "Three", '4': "Four", '5': "Five", '6': "Six", '7': "Seven", '8': "Eight", '9': "Nine"}

And finally, I want it to print the dictionary values that correspond to the keys in the tuple. I'm pretty sure this is where I'm getting it wrong.

print(numWords[numTup])

Essentially what I'm trying to do with this program is have it print each user inputted digit as a word, ie. 456 would turn into "four five six".

The full (incorrect) script:

num = input("Enter a number here: ")
numTup = tuple(num)
numWords = {'1': "One", '2': "Two", '3': "Three", '4': "Four", '5': "Five", '6': "Six", '7': "Seven", '8': "Eight", '9': "Nine"}
print(numWords[numTup])

Upvotes: 1

Views: 2804

Answers (4)

The tuple is not necessary in this situation because your dictionary will handle assigning the keys to the associated strings.

num = input("Enter a number here: ")

numWords = {'1': "One", '2': "Two", '3': "Three", '4': "Four", '5': "Five", '6': "Six", '7': "Seven", '8': "Eight", '9': "Nine"}
for n in num:
    print(numWords[n], end=' ')

Demo:

Enter a number here: 456
Four Five Six

Upvotes: 3

Agarp
Agarp

Reputation: 433

'''
You can print the number in words given irs numeric
version if by defining your dictionary with
the following format:
    dict{'number': 'word'}

Then, calling the dictionary as follows:
    dict['number']
'''

# Number (keys) and words (values) dictionary
numWords = {'1': "One", '2': "Two", 
'3': "Three", '4': "Four", 
'5': "Five", '6': "Six", 
'7': "Seven", '8': "Eight", 
'9': "Nine", '10': "Ten"}

# Function that prints the number in words
def print_values():
    for k,v in numWords.items():
        print(v)

'''
Alternatively, if you want to print a value using
its key as an argument, you can use the following
funciton:
'''

# Interactive function to print number in words
# given the number
def print_values2():
    key = input("What number would you like to print? ")
    print(numWords[key])

'''
P.s. if you want to add a number to your dictionary
you can use the following function
'''

# Function to modify numWords
def add_number():

    # Specify the number you want to add
    numeric = input("Type in the number: ")

    # Input the number in words
    word = input("Write the number in words: ")

    # Assign number and word to dictionary
    numWords[numeric] = word

    # Return modified dictionary
    return numWords

Upvotes: 0

Sanyam Mehra
Sanyam Mehra

Reputation: 309

There is a mismatch between the type of the keys of your dict and the input after you convert it to a tuple with tuple(num). You could either just skip the part where you convert to tuple:

num = input("Enter number here: ")
num = input("Enter a number here: ")
numWords = {'1': "One", '2': "Two", '3': "Three", '4': "Four", '5': "Five", '6': "Six", '7': "Seven", '8': "Eight", '9': "Nine"}
print(numWords[num])

OR

index into the tuple and access the element by picking the 0th element:

num = input("Enter number here: ")
num = input("Enter a number here: ")
numTup = tuple(num)
numWords = {'1': "One", '2': "Two", '3': "Three", '4': "Four", '5': "Five", '6': "Six", '7': "Seven", '8': "Eight", '9': "Nine"}
print(numWords[numTup[0]])

NOTE: Make sure the datatype of the keys and the variable you are using to access the dict items is the same. You can check the data type of a variable with the type() command.

Upvotes: 0

Fady Saad
Fady Saad

Reputation: 1189

It is not clear why you want to turn the number into tuple but in case you want that, you did in a wrong way. when you want to create a tuple from an int you should do the following:

numTup = (num)

And your full code should looks like:

num = input("Enter a number here: ")
numTup = (num)
numWords = {'1': "One", '2': "Two", '3': "Three", '4': "Four", '5': "Five", '6': "Six", '7': "Seven", '8': "Eight", '9': "Nine"}
print(numWords[numTup])

Upvotes: -1

Related Questions