2017-10-09 38 views
-1

我只是將此代碼轉換爲一個整數(儘管我實際上並未使用整數)到一個字符串。例如,45 - >四十五。它完美的工作,但代碼很長。有什麼方法可以縮短它嗎?將2位數字轉換爲python中的字符串

num = input("Enter a two digit number: ") 

first_digit = num[0] 
if first_digit == "2": 
    first_digit_str = "Twenty" 
elif first_digit == "3": 
    first_digit_str = "Thirty" 
elif first_digit == "4": 
    first_digit_str = "Forty" 
elif first_digit == "5": 
    first_digit_str = "Fifty" 
elif first_digit == "6": 
    first_digit_str = "Sixty" 
elif first_digit == "7": 
    first_digit_str = "Seventy" 
elif first_digit == "8": 
    first_digit_str = "Eighty" 
elif first_digit == "9": 
    first_digit_str = "Ninety" 

second_digit = num[1] 
if second_digit == "1": 
    second_digit_str = "-One" 
elif second_digit == "2": 
    second_digit_str = "-Two" 
elif second_digit == "3": 
    second_digit_str = "-Three" 
elif second_digit == "4": 
    second_digit_str = "-Four" 
elif second_digit == "5": 
    second_digit_str = "-Five" 
elif second_digit == "6": 
    second_digit_str = "-Six" 
elif second_digit == "7": 
    second_digit_str = "-Seven" 
elif second_digit == "8": 
    second_digit_str = "-Eight" 
elif second_digit == "9": 
    second_digit_str = "-Nine" 

if first_digit == "1": 
    if second_digit == "1": 
     print("Eleven") 
    elif second_digit == "2": 
     print("Twelve") 
    elif second_digit == "3": 
     print("Thirteen") 
    elif second_digit == "4": 
     print("Fourteen") 
    elif second_digit == "5": 
     print("Fifteen") 
    elif second_digit == "6": 
     print("Sixteen") 
    elif second_digit == "7": 
     print("Seventeen") 
    elif second_digit == "8": 
     print("Eighteen") 
    elif second_digit == "9": 
     print("Nineteen") 

else: 
    print(first_digit_str + second_digit_str) 

k = input("Press enter to close.") 
+0

我很抱歉,我不能設法通常張貼問題。 – Faradrim

+0

你可以將每個數字映射到相應的單詞上。 –

+0

你可以適應這個更一般的基於Java的解決方案:https://stackoverflow.com/a/3299672/684776 – mac9416

回答

0

隨着n2w庫:

安裝(從命令行):

pip install n2w 

用法:

import n2w 

num = 45 
num_words = '-'.join(w.capitalize() for w in n2w.convert(num).split()) 

print(num_words) 

的輸出:

Forty-Five 

https://github.com/collin5/python-n2w

+0

這是一個很好的答案,但我是Python的新手,並不知道這意味着什麼。你能否解釋這實際上意味着什麼? – Faradrim

+0

@Faradrim,確實得到了「線索」 - 你應該按照我發佈的鏈接,然後,按照我的安裝說明 – RomanPerekhrest

+0

我道歉,我現在已經看了鏈接。 – Faradrim

0

可以參考n2w包 用法:

從N2W進口轉換

結果=轉換(123456)

或ch eck此爲detailed ans

+0

我看看n2w是什麼,它只是佈局,我不會從使用它學到任何東西。好的答案,但是。 – Faradrim

0

這是一個有點短:

num_int = input("Enter a two digit number: ") 

above_19 = { "2":"Twenty", "3":"Thirty", "4": "Forty", "5": "Fifty", 
       "6": "Sixty" , "7": "Seventy", "8": "Eighty", "9": "Ninety"} 

above_10 = { "1":"Eleven", "2":"Twelve", "3":"Thirteen", "4":"Fourteen", 
       "5": "Fifteen" , "6": "Sixteen", "7":"Seventeen", "8": 
       "Eighteen", 9:"Nineteen"} 

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

num = str(num_int) 
first_digit = num[0] 
second_digit = num[1] 
if (first_digit == "1"): 
    result = above_10[second_digit] 
else: 
    result = above_19[first_digit]+digit_2[second_digit]