简体   繁体   中英

Converting a 2 digit number to string in python

I just made this code to convert an integer (although I don't actually use an integer) to a string. For example, 45 --> Forty-Five. It works perfectly but the code is quite lengthy. Is there any way that I could shorten it?

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.")

With n2w library:

Installation (from command line):

pip install n2w

Usage:

import n2w

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

print(num_words)

The output:

Forty-Five

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

You can refer n2w package Usage :

from n2w import convert

result = convert(123456)

Or check this for detailed ans .

This is a little bit shorter:

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]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM