繁体   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]?

所以我想把一个字符串改成一个整数,然后让控制台用正确的逗号打印数字,

所以字符串可以是“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()

但是当我运行代码并输入“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()

现在打印 0。 您必须将 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(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()

您可以将 data[1] 更改为带逗号的字符串

你可以在你的 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"

我会使用与 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()

这种方法具有很强的可扩展性; 您可以通过扩展替换字典来添加使用“billion”、“trillion”等的能力。

在进一步扩展这一点时,想想如果用户输入,例如“12000”、“170 万”或“200 万 54.3 万 819”,您可能想要什么?

暂无
暂无

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

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