Reputation: 31
How can i set default values on the w, d & h?
w, d, h = input("Specify width, depth & height: ").split()
print (w)
print (d)
print (h)
Upvotes: 1
Views: 335
Reputation: 12140
Explicit is better than implicit:
values = input("Specify width, depth & height: ").split()
w = values[0] if len(values) > 0 else DEFAULT_W
d = values[1] if len(values) > 1 else DEFAULT_D
h = values[2] if len(values) > 2 else DEFAULT_H
But a more extravagant way could be:
values = iter(input("Specify width, depth & height: ").split())
w = next(values, DEFAULT_W)
d = next(values, DEFAULT_D)
h = next(values, DEFAULT_H)
Upvotes: 1
Reputation: 428
w, d, h = input("Specify width, depth & height: ").split() or (10, 11, 12)
print (w)
print (d)
print (h)
This sets the values to a default if the user entered nothing, since an empty string will be split to an empty list, which will be evaluated to False
.
Upvotes: 0