我是 python 的新手,但我会尽力解释。我有一个像这样的输入 1234567890,我希望它在更大的数字时更具可读性,我希望它的格式像这样 1,234,567,890。
from datetime import datetime
def price_calc():
the_item = 38
amount_of_the_item = input("Amount of items: ") #This output is what i want to change.
price = ((int(amount_of_the_item)) * (int(the_item)))
print("{:,}".format(price),"USD")
now = datetime.now()
t = now.strftime("%H:%M:%S")
print("Time", t)
while True:
price_calc()
现在我从控制台得到这个:
Amount of items: 1234567890
46,913,579,820 USD
Time 01:22:29
但我想得到这个:
Amount of items: 1,234,567,890
46,913,579,820 USD
Time 01:22:29
控制台输出的第一行是我要更改的内容。
请您参考如下方法:
我以某种方式解决了这个问题,所以用户的输入在它出现在屏幕上之前就从终端被清除了,我仍然有这个值
旧代码
from datetime import datetime
def price_calc():
the_item = 38
amount_of_the_item = input("Amount of items: ") #This output is what i want to change.
price = ((int(amount_of_the_item)) * (int(the_item)))
print("{:,}".format(price),"USD")
now = datetime.now()
t = now.strftime("%H:%M:%S")
print("Time", t)
while True:
price_calc()
新代码
import os #NEW
from datetime import datetime
def price_calc():
os.system("cls") #NEW
the_item = 38
amount_of_the_item = input("Amount of items: ")
print('\033[1A[\033[2K\033[1G', end='') #NEW
print('Amount of items: {:,}'.format(int(amount_of_the_item))) #NEW
price = ((int(amount_of_the_item)) * (int(the_item)))
print("{:,}".format(price),"USD")
now = datetime.now()
t = now.strftime("%H:%M:%S")
print("Time", t)
input("Press Enter To Continue") #NEW. This is a buffer so the code dont clear it self before i can read it.
while True:
price_calc()
我现在得到的控制台输出是这样的:
元素数量:1,234,567,890 46,913,579,820 美元 时间 15:15:59 按 Enter 键继续
谢谢你给我的所有帮助。
