2017-09-16 13 views
1

プログラミングにはとても新しいので、何かが意味をなさない場合や私が誤って単語を言いたい場合は、私を許してください。タプルを辞書のキーとして使うことについての質問があります。Pythonでキーとしてタプルを使用する

numTup = tuple(num) 

次に、私は単語の数値キーを接続する辞書を作成する:

まず、私はタプルにこの数値を入れ、そしてユーザ入力を数、

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"} 

最後に、私はそれに対応する辞書値を印刷したいタプルのキー。私はこれが間違っているところだと確信しています。

私はこのプログラムで何をしようとしているのかということは、各ユーザが入力した数字を単語として印刷することです。 456は "four five five six"に変わる。

フル(間違った)スクリプト:

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

なぜこれらをタプルに変換するのかは不明です。あなたの辞書のキーは文字列であり、数字やタプルではありません。あなたが 'input'から得るものは文字列でもあります。何かに変換することなく、設定した方法でキーとして直接使用することができます。 – pvg

+0

ああ、あなたが何をしているのか分かりますが、入力の各文字をキーとして使用する必要があります。しかし、タプル自体をキーとして辞書検索をしようとしています。だから、タプルが '( '3'、 '1'、 '0')'ならば、あなたの検索は正確なキーを探していて、 '3'、' 1'、 '0'のキーではありません。タプルの値を繰り返し処理し、それらの値(文字列)をキーとして使用する必要があります。あなたの辞書はキーのための文字列を持っているからです。 – pvg

+0

タプルは実際には余計ですが、入力した文字列の文字を繰り返し処理できます。 'for digit in inputstring:'はこのようなループを開始します。ループの中で 'numWords [digit] 'を印刷するだけです。 – pvg

答えて

3

あなたの辞書がするのでtupleは、このような状況では必要ありません関連する文字列にキーを割り当てます。

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=' ') 

デモ:

Enter a number here: 456 
Four Five Six 
+0

リストはタプルよりも必要ではないようです。 – pvg

+0

確かにあなたは正しいよ – davedwards

+0

'splainin'を追加したいかもしれません。 – pvg

-1

あなたはタプルの中にいますが、あなたは間違った方法でやった、ことをしたい場合には番号を変えたい理由は明らかではありません。あなたがintからタプルを作成する場合は、次の作業を行う必要があります。

numTup = (num) 

そして、あなたの完全なコードは次のようになります必要があります。

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

あなたのdictのキーのtype、あなたがtuple(num)とタプルに変換した後の入力の間に不一致があります。 あなたはどちらかだけでは、タプルに変換どこの部分をスキップすることができます:タプルに

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

指数と0番目の要素選ぶことによって要素にアクセス:

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

注: dict項目にアクセスするために使用するキーと変数のデータ型が同じであることを確認してください。 type()コマンドで変数のデータ型をチェックすることができます。

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

これは、この質問に対するパロディエンタープライズソリューションのようなものです。 – pvg

関連する問題