0

I want to have a default response using python input function i would like it to behave like so

response = input("Enter your response :", default="default response")

that would prompt the user : Enter your response : default response where "default response" behave like if the user writted it so he can modify a text already written.

did'nt found anything about that in the docs but i guess it's possible

MR.Glass
  • 11
  • 1
  • 1
    Does this answer your question? [How to define default value if empty user input in Python?](https://stackoverflow.com/questions/22402548/how-to-define-default-value-if-empty-user-input-in-python) – Timur Shtatland Mar 10 '23 at 16:24
  • A Python module which provides a GUI interface such as PySimpleGUI makes it fairly easy to have a default input which the user can accept, edit or replace, – user19077881 Mar 10 '23 at 16:47

2 Answers2

2

Looks like there's no way to give the user a pre-typed default value. But what you can do is prompt the user for input with the default value specified. If the user continues without entering anything then the code will consider the default value. THOUGH IT IS NOT A GOOD PRACTICE

For example,

age = input("Enter age or just press Return to accept the default of 18: ") or "18"
1

Use or operator

If the user just press enter, an empty string will be returned. The truthiness of an empty string is false, so the right operand will be evaluated and returned.

response = input('Enter your response ["default response"]:') or "default response"

For this behavior:

Enter your response : default response where "default response" behave like if the user writted it so he can modify a text already written.

No, it's not possible as far as I know.

About or returned value

or doesn't return a bool but the first True value or the right operand if both are False:

>>> 10 or 42
10
>>> '' or 42
42
>>> 0 or ''
''
>>> '' or 0
0
Dorian Turba
  • 3,260
  • 3
  • 23
  • 67