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.

7
  • 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. Commented Aug 15, 2019 at 23:43
  • This question is a duplicate of stackoverflow.com/questions/1823058/… Commented Aug 15, 2019 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. Commented Aug 15, 2019 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. Commented Aug 16, 2019 at 0: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: code.activestate.com/recipes/134892 Commented Aug 16, 2019 at 0:31

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.