2

I'm very new to python but i will try to explain. I have a input like this 1234567890 and i want it to be more readable when it is a larger number and i want it to be formatted like this 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()

Right now I get this from the console:

Amount of items: 1234567890
46,913,579,820 USD
Time 01:22:29

But I want to get this instead:

Amount of items: 1,234,567,890
46,913,579,820 USD
Time 01:22:29

and the first line of the console output is what I want to change.

Rory Daulton
  • 21,934
  • 6
  • 42
  • 50
GabbeHags
  • 101
  • 1
  • 2
  • 8
  • 2
    You can't easily change the appearance of the user input as they are providing it, but you can just call `print("Amount of items: {:,}".format(amount_of_the_items))` after the `input` statement to present the formatted version. – metatoaster Aug 15 '19 at 23:43
  • This question is a duplicate of https://stackoverflow.com/questions/1823058/how-to-print-number-with-commas-as-thousands-separators – Grismar Aug 15 '19 at 23:44
  • @Grismar OP wants to change the appearance of what appears on the `input` call itself; you can see they already use your linked solution for the `USD` line, so that is not the correct duplicate. – metatoaster Aug 15 '19 at 23:46
  • Ah apologies, you're right - thanks. I'm however not allowed to change my vote unless the question is edited. I'll retract the vote once I can. – Grismar Aug 16 '19 at 00:03
  • 1
    Having looked into it for a bit, I think you'll only be able to do it if you read input one character at a time and manage printing of the input yourself. This is going to be a lot of work for something that's primarily cosmetic. I'd recommend parsing the input so that it accepts both `1000000` and `1,000,000` and leaving it up to the user. But if you insist on doing all the work, this might be a good starting point: http://code.activestate.com/recipes/134892/ – Grismar Aug 16 '19 at 00:31
  • Does the OP really expect to modify input as it is typed? The way the question is written, it might also mean they just want to edit the input after it's entered. If so, then it can done with ANSI escapes, like this: `amount_of_the_item = input("Amount of items: "); print('\033[1A[\033[2K\033[1G', end=''); print('Amount of items: {:,}'.format(int(amount_of_the_item)))`. – ekhumoro Aug 16 '19 at 01:00
  • I have solved it in a way but not as I want it, but i did ``os.system("cls")`` before I set the value of ``the_item = 38`` and then i use the code that @ekhumoro wrote. – GabbeHags Aug 16 '19 at 10:02

1 Answers1

1

I solved the problem in a way so the input of the user gets cleard from the terminal before it shows up on the screen and I still have the value

OLD CODE

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()

NEW CODE

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()

The console output i get now is this:

Amount of items: 1,234,567,890 46,913,579,820 USD Time 15:15:59 Press Enter To Continue

Thank you for all the help i got.

GabbeHags
  • 101
  • 1
  • 2
  • 8