2017-09-16 3 views
1

저는 프로그래밍에 익숙하지 않아 무엇이든 이해할 수 없거나 잘못 말하면 용서해줍니다. 튜플을 사전 키로 사용하는 것에 대한 질문이 있습니다.튜플을 파이썬에서 키로 사용하기

numTup = tuple(num) 

다음에, I는 단어 숫자 키를 연결하는 사전을 생성 :

먼저, I는 I 튜플에이 숫자 값을 돌린 다음 사용자 입력을 다수

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

마지막으로 나는 t에 해당하는 사전 값을 인쇄하고 싶습니다. 튜플의 키. 나는 이것이 내가 잘못 이해하고있는 곳이라고 확신한다.

기본적으로이 프로그램을 사용하여 수행하려는 작업은 각 사용자가 입력 한 숫자를 한 단어로 인쇄하도록하는 것입니다. 456은 "4 5 6"으로 바뀔 것입니다.

전체 (잘못된) 스크립트 : 당신이 튜플로 수를 설정하려는 이유

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에있는 키는 문자열이나 숫자 나 튜플이 아닙니다. '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=' ') 

데모 : 당신의 딕셔너리의 키의 type하고 tuple(num)와 튜플로 변환 한 후 입력 사이에 불일치가있다

Enter a number here: 456 
Four Five Six 
+0

목록이 튜플보다 더 이상 필요하지 않은 것처럼 보입니다. – pvg

+0

실제로 당신 말이 맞습니다 – davedwards

+0

'splainin'을 추가하고 싶을 수도 있습니다. – pvg

-1

는 분명하지 않지만 경우에 당신은 당신이 잘못된 길에서했던 것을 원한다.

numTup = (num) 

그리고 당신의 전체 코드는해야는 다음과 같습니다 : 당신은 INT에서 튜플을 만들 때 다음을 수행해야이 상황에서

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

. 당신도 당신이 튜플로 변환 어디 부분을 건너 뛸 수 : 튜플에

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

또는

지수와 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

관련 문제