Reputation: 31
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
Upvotes: 1
Views: 1354
Reputation: 124
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.
It is worth noting though that it may not be obvious behavior for people less techie. You may want to consider adding some sort of explanation that pressing return fills in the default value.
For example,
age = input("Enter age or just press Return to accept the default of 18: ") or "18"
Upvotes: 4
Reputation: 3755
or
operatorIf 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.
or
returned valueor
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
Upvotes: 0