简体   繁体   中英

I'm trying something new with python but isn't working, can anyone help me figure out why the function only prints data[0]?

so I'm trying to change a word string into an integer, and then have the console print the number with the correct commas,

so the string can be "2 million", but I want the console to print 2,000,000 as an integer, including the comma separators,so far this is code I have;

def main():
    print("Please enter a number using the word 'thousand' or 'million'.")
    number = input("For example, 1 million or 1 thousand: ")
    data = number.split(" ")
    if data[1] == str.lower('thousand'):
        data[1] = 000
        reply = (int(data[0]) + data[1])
        print(f"{reply:,}")

main()

but when I run the code and input "2 thousand", the console just prints the number 2, no zeroes or commas, where am I going wrong?

def main():
    print("Please enter a number using the word 'thousand' or 'million'.")
    number = input("For example, 1 million or 1 thousand: ")
    data = number.split(" ")
    if data[1] == str.lower('thousand'):
        data[1] = ',000'
        reply = ((data[0]) + data[1])
        # print(f"{reply:,}")
        print(reply)
main()

0s are now printed. You have to add 000 as string.

or

def main():
    print("Please enter a number using the word 'thousand' or 'million'.")
    number = input("For example, 1 million or 1 thousand: ")
    data = number.split(" ")
    if data[1] == str.lower('thousand'):
        data[1] = '000'
        reply = int(str(data[0])+data[1])
        numbers = "{:,}".format(reply)
        # print(f"{reply:,}")
        print(numbers)
main()
def main():
    print("Please enter a number using the word 'thousand' or 'million'.")
    number = input("For example, 1 million or 1 thousand: ")
    data = number.split(" ")
    if data[1] == 'thousand':
        data[1] = ',000'
        reply = ((data[0]) + data[1])
        # print(f"{reply:,}")
        print(reply)
    elif data[1] == str.lower('million'):
        data[1] = ',000,000'
        reply = ((data[0]) + data[1])
        # print(f"{reply:,}")
        print(reply)
main()

You can change data[1] to a string with commas

you can just replace it in your str :

def main():
    print("Please enter a number using the word 'thousand' or 'million'.")
    number = input("For example, 1 million or 1 thousand: ")
    str_number={
        "thousand":"000",
        "million":"000,000"
    }
    print(','.join([str_number[e] if e in str_number else e for e in number.split(' ')]))    

main()

如果你想使用整数,你可以这样做:

        reply = int(data[0]) * 1000 # or 1000000 if "million"

I'd use a similar approach to Satyam Shankar, but perhaps with a dictionary to dynamically replace "million" or "thousand" with the appropriate string:

def main():
    replacement = {"thousand":",000",\
                   "million" :",000,000"
                  }
    print("Please enter a number using the word 'thousand' or 'million'.")
    number = input("For example, 1 million or 1 thousand: ")
    data = number.split(" ")
    if data[1] == str.lower('thousand'):
        data[1] = replacement[data[1]]
        reply = ((data[0]) + data[1])
        # print(f"{reply:,}")
        print(reply)
main()

This approach is quite extensible; you could add the ability to use "billion", "trillion", etc. by extending the replacement dictionary.

In extending this further, think about what about you might want if the user inputs, for example, "twelve thousand", "1.7 million" or "2 million 543 thousand 819"?

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