Here I have to set the default value if the user will enter the value from keyboard. Here is the code that user can enter value:
input= int(raw_input("Enter the inputs : "))
here the value will assign to variable input
after entering value and hit 'Enter', is there any method that if we don't enter value and directly hit the 'Enter' key and the variable will directly assign default value say as input = 0.025
.
You could first input a string, then check for zero length and valid number:
One of the way is -
Another way can be -
How does it work?
If nothing was entered then raw_input returns empty string. Empty string in python is
False
bool("") -> False
. Operatoror
returns first trufy value, which in this case is"42"
.This is not sophisticated input validation, because user can enter anything, e.g. ten space symbols, which then would be
True
.Most of the above answers are correct but for Python 3.7, here is what you can do to set the default value.
You can do it like this: