0

I would like to have a default value in the user input cursor.

option = input("Chose an option 1. run, 0. exit to run the output: ") or 1

I have tried the above code, but it didn't work for me.

When I run the below example code. Example:

option = input("Chose an option 1. run, 0. exit to run the output: ")

Expected output:

Chose an option 1. run, 0. exit to run the output: 1

By default 1 should be listed in the user input cursor, either user can enter directly or modify it before pressing enter key.

Vishwas
  • 343
  • 2
  • 13
ArunPrasath
  • 153
  • 4
  • 17
  • Good question. I really don't know if this is possible.. – Akaisteph7 Jul 22 '19 at 03:26
  • 1
    This isn't possible with the built-in input() function. See: https://stackoverflow.com/questions/2533120/show-default-value-for-editing-on-python-input-possible/2533134 – Tim Jul 22 '19 at 03:52
  • Possible duplicate of [How to put text in input line: how to ask for user input on the command line while providing a 'default' answer that the user can edit or delete?](https://stackoverflow.com/questions/30193945/how-to-put-text-in-input-line-how-to-ask-for-user-input-on-the-command-line-whi) – Akaisteph7 Jul 22 '19 at 14:09
  • Even the reference URL did not help. If you have tried the solution please let me know – ArunPrasath Jul 25 '19 at 06:38

2 Answers2

0

rx7 module has an function for this question.

(This only work on windows)

import rx7
rx7.Input(prompt, default_value)
# e.g
rx7.Input("Chose an option 1. run, 0. exit to run the output: ",'1')

(I know It's been 11 months since your question but still hope it helps you)

Ramin-RX7
  • 128
  • 1
  • 1
  • 13
0
def prompt(prompt, default_value):
    answer = input(prompt + ' ' + default_value + '\r' + prompt + ' ')
    if answer == "": answer = default_value
    return answer

choice = prompt('Do you want to clear the contents of this folder [Y/n]?', 'Y')

The secret here is \r which will make the cursor go back to the start of the line without overwriting the text. You then overwrite the text with the prompt without printing the default value et voila ... If the answer is an empty string, the we return the default_value.

intGod
  • 1
  • 1