我对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
控制台输出的第一行是我要更改的内容。
答案 0 :(得分:1)
我以某种方式解决了问题,使用户的输入在显示在屏幕上之前就已经从终端清除了,我仍然有值
旧代码
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()
我现在得到的控制台输出是:
Amount of items: 1,234,567,890
46,913,579,820 USD
Time 15:15:59
Press Enter To Continue
感谢您提供的所有帮助。