Peter N
Peter N

Reputation: 31

Default value when using multiple inputs

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

Answers (2)

Yevhen Kuzmovych
Yevhen Kuzmovych

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

Jotha
Jotha

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

Related Questions