0

I would like to add a "default value" to an input().

Something like this:

>>> x = input("Enter a number: ", value="0")
Enter a number: 0

So that the user can edit it, like the value attribute in html <input>

I tried using pyautogui:

import pyautogui

def input_value():
    x = input("Enter a number: ")
    pyautogui.hotkey("0")return x

But naturally it doesn't work because pyautogui.hotkey() is called after input().

Is there any way to do this?

Elvis
  • 1
  • 2
  • `input` just reads from standard input, which may not even be an interactive terminal. – chepner Mar 24 '22 at 21:49
  • The problem with your approach is, the user has to edit the default input. Instead would you consider to use something like this: `Do you want to continue? [Y/n]` (It's commonly used). Here uppercase value is default value. (Some times you put the default value in parenthesis). Some other examples can be `frame to be written into (1:16) (2):` Here you need to choose between [1-16] and if you hit the return without any number it will be considered as `2`. – MSH Mar 24 '22 at 21:57
  • @MSH I put a casual code, but the reason of my question is that I'm trying to create a "database" (with pickle) of jokes, so if somebody chooses the option "Edit" he must rewrite all the joke. Instead, with a thing like a "default value", he has just to edit the text that already is present. – Elvis Mar 24 '22 at 22:31

1 Answers1

0

Take a look at pyinputplus for enhanced input() functionality. If the online documentation isn't enough, you can also read Automate the Boring Stuff with Python for a good guide on using the module

https://pypi.org/project/PyInputPlus/

All input functions have the following parameters: default (str, None): A default value to use should the user time out or exceed the number of tries to enter valid input.

WAHISHON
  • 17
  • 4