1

I want input data in python but while inputting data,on the right side must be output for example, kilogram:

simple code:

weight = float(input('Enter your weight: (kg)'))

Output:

Enter your weight: some_number (kg) 

I want that kg stands always on right side of number while inputting data. I think question is clear if something not please let me know.Thank you in advance!

CristiFati
  • 38,250
  • 9
  • 50
  • 87
  • 3
    You can print your prompt, some spaces and "kg" and then move the cursor back to those spaces. Test this and adjust your string: `print("aaaaaaaaaaaaaa\r\tbb")` - `\r` is \carriage return\ which gets you back to the beginning(!) of the current line, `\t` is a tab character. – h4z3 Aug 01 '19 at 10:16
  • 1
    @h4z3 do note that if you are using something like pycharm, the console window is emulated so it wont show up correctly – Nullman Aug 01 '19 at 10:21
  • 2
    Possible duplicate of [Python: How do I get user input in the middle of a sentence (fill-in-the-blank style) using the input function?](https://stackoverflow.com/questions/38246529/python-how-do-i-get-user-input-in-the-middle-of-a-sentence-fill-in-the-blank-s) – malioboro Aug 01 '19 at 10:23
  • 1
    why can't you just put the units in you input like this `weight = float(input('Enter your weight (kg): '))`? – user7440787 Aug 01 '19 at 10:31
  • 1
    in console's programs it is natural to have `(kg)` on left before `:` - `Enter your weight (kg): some_number`. – furas Aug 01 '19 at 10:39
  • user7440787, Thank you friends ! I want that kg must be on the right side numbers that I am entering. For example Enter your weight:60(kg). –  Aug 01 '19 at 10:40
  • 1
    maybe with modules [curses](https://docs.python.org/3.7/library/curses.html), [npyscreen](https://npyscreen.readthedocs.io/introduction.html), [urwind](http://urwid.org/) you could do this - but not all consoles have functions to put text in selected place. – furas Aug 01 '19 at 10:44
  • Agree with @furas you, take a look at `curses` – trsvchn Aug 01 '19 at 10:48

1 Answers1

1

If you dig around, you'll find various versions of something called getch. This uses code from py-getch:

import sys

try:
    from msvcrt import getch
except ImportError:
    def getch():
        import tty
        import termios
        fd = sys.stdin.fileno()
        old = termios.tcgetattr(fd)
        try:
            tty.setraw(fd)
            return sys.stdin.read(1)
        finally:
            termios.tcsetattr(fd, termios.TCSADRAIN, old)

ascii_numbers = [ord('0') + x for x in range(10)]
weight = 0.0
while True:
    message = f"Enter your weight: {weight:9.2f} (kg)"
    sys.stdout.write("\r" + message)
    sys.stdout.flush()
    c = ord(getch())
    if c == 13:
        break
    elif c in ascii_numbers:
        c = c - ord('0')
        weight = (weight * 10) + (float(c) / 100)
    elif c == 127:
        weight = weight / 10
print("")

This is ugly, but my last experience with ncurses was even uglier.

Warning

This program ignores Ctrl-C inside the getch call. It is possible to modify this code so that the only way to stop the program is to kill the process. Sorry.