简体   繁体   English

我正在用 python 尝试一些新的东西,但没有用,谁能帮我弄清楚为什么这个函数只打印 data[0]?

[英]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;所以字符串可以是“200万”,但我希望控制台将 2,000,000 作为整数打印,包括逗号分隔符,到目前为止这是我的代码;

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?但是当我运行代码并输入“2000”时,控制台只打印数字 2,没有零或逗号,我哪里出错了?

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.现在打印 0。 You have to add 000 as string.您必须将 000 添加为字符串。

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您可以将 data[1] 更改为带逗号的字符串

you can just replace it in your str :你可以在你的 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:我会使用与 Satyam Shankar 类似的方法,但也许使用字典来动态替换“百万”或“千”为适当的字符串:

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.您可以通过扩展替换字典来添加使用“billion”、“trillion”等的能力。

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"?在进一步扩展这一点时,想想如果用户输入,例如“12000”、“170 万”或“200 万 54.3 万 819”,您可能想要什么?

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 任何人都可以帮助我找出为什么我使用plot.hist函数得到的法线概率密度图有误 - Anyone can help me figure out why I got a wrong normal probability density figure with plot.hist function 我是 python 的新手,我正在努力学习它,我不明白为什么一个结果给了我一个不同的结果 - Im new to python and am trying to learn it, I can't figure out why one outcome is giving me a different one 试图为我的 python 类完成这个程序,但无法弄清楚为什么我会收到 TypeError - Trying to finish this program for my python class, but can't figure out why I'm receiving a TypeError 无法弄清楚为什么我只能从预测函数中得到一个值? - Can't figure out why I'm only getting one value from predict function? 列的长度必须与键相同。 为什么我会收到这样的错误? 谁能帮我吗? - Columns must be same length as key. Why I'm getting such error? can anyone help me out? 我正在尝试编写二十一点游戏,有人可以帮助我解决我的分数检查解决方案吗? Python - I’m trying to code a blackjack game, can anyone help me with my score-checking solution? Python 函数names()和testCraps()函数无法正常工作。 谁能找出原因? - function names() and function testCraps() aren't working correctly. Can anyone figure out why? 试图找出为什么某些输出不起作用 - Trying to figure out why some of the output isn't working 谁能向我解释为什么这个 apply() 方法不起作用? - can anyone explain to me why this apply() method isn't working? 将Function嵌套在python中,运行时结果是NameError。 谁能帮我? - Nested Function in python, when I run it, the result came out is NameError. Can anyone help me?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM