1

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])
5
  • It's not clear why you are turning these into tuples. The keys in your dict are strings, not numbers or tuples. The stuff you get from input is also a string. You can use it as a key directly, the way you've set it up, without converting it to anything. Commented Sep 16, 2017 at 0:44
  • Oh, I see what you're after, you want each character from the input to be used as a key. But you are trying to do dictionary lookup with the tuple itself as a key. So if a tuple is ('3', '1', '0'), your lookup is looking for that exact key, not for the keys 3, 1, 0. You need to iterate over the the values of the tuple and use those values (which are strings) as keys. Since your dictionary has strings for keys. Commented Sep 16, 2017 at 0:54
  • So, the tuple is actually superfluous here, you can simply iterate over the characters of the string you got as input. so for digit in inputstring: will start just such a loop. then in the loop you can just print numWords[digit] Commented Sep 16, 2017 at 0:56
  • Thank you so much for the explanation! Not sure what I was trying to do lol Commented Sep 16, 2017 at 1:08
  • Np. The missing bit was the iteration over the elements of the tuple - it would have still worked with the tuple as you had it. But you just happen to not need it and python makes it easy to iterate over the characters of a string. Commented Sep 16, 2017 at 1:11

4 Answers 4

3

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
Sign up to request clarification or add additional context in comments.

8 Comments

The list seems no more necessary than the tuple.
Might want to also add some 'splainin'.
Thank you, this does the job! I was unclear on the purpose of a tuple and thought I had to use one for this process.
true, @pvg I didn't expect this to work in comparison to the other answers, will try to include some verbage :)
@downshift, num return a string, in order to use it for indexing a dictionary, it need to be converted to int first. Therefore print(numWords[n]) should be print(numWords[int(n)], end = ' ').
|
0

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.

Comments

0
'''
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

1 Comment

This reads a bit like the parody enterprise solution to this question.
-1

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])

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.