Parameter to disallow zero in input_int
This commit is contained in:
parent
ac3ab8099b
commit
33a9f12ac0
|
@ -129,9 +129,11 @@ class AdjustStockMenu(Menu):
|
||||||
print(f'The stock of this product is: {product.stock:d}')
|
print(f'The stock of this product is: {product.stock:d}')
|
||||||
print('Write the number of products you have added to the stock')
|
print('Write the number of products you have added to the stock')
|
||||||
print('Alternatively, correct the stock for any mistakes')
|
print('Alternatively, correct the stock for any mistakes')
|
||||||
add_stock = self.input_int('Added stock', allowed_range=(-1000, 1000))
|
add_stock = self.input_int('Added stock', allowed_range=(-1000, 1000), zero_allowed=False)
|
||||||
# TODO: Print something else when adding negative stock?
|
if add_stock > 0:
|
||||||
print(f'You added {add_stock:d} to the stock of {product}')
|
print(f'You added {add_stock:d} to the stock of {product}')
|
||||||
|
else:
|
||||||
|
print(f'You removed {add_stock:d} from the stock of {product}')
|
||||||
|
|
||||||
product.stock += add_stock
|
product.stock += add_stock
|
||||||
|
|
||||||
|
|
|
@ -200,13 +200,15 @@ class Menu(object):
|
||||||
def invalid_menu_choice(self, in_str):
|
def invalid_menu_choice(self, in_str):
|
||||||
print('Please enter a valid choice.')
|
print('Please enter a valid choice.')
|
||||||
|
|
||||||
def input_int(self, prompt=None, end_prompt=None, allowed_range=(None, None), null_allowed=False, default=None):
|
def input_int(self, prompt=None, end_prompt=None, allowed_range=(None, None), null_allowed=False, zero_allowed=True,
|
||||||
|
default=None):
|
||||||
while True:
|
while True:
|
||||||
result = self.input_str(prompt, end_prompt, default=default)
|
result = self.input_str(prompt, end_prompt, default=default)
|
||||||
if result == '' and null_allowed:
|
if result == '' and null_allowed:
|
||||||
return False
|
return False
|
||||||
try:
|
try:
|
||||||
value = int(result)
|
value = int(result)
|
||||||
|
# TODO: Should this be turned into greater-than-equals and less-than-equals?
|
||||||
if ((allowed_range[0] and value < allowed_range[0]) or (allowed_range[1] and value > allowed_range[1])):
|
if ((allowed_range[0] and value < allowed_range[0]) or (allowed_range[1] and value > allowed_range[1])):
|
||||||
if allowed_range[0] and allowed_range[1]:
|
if allowed_range[0] and allowed_range[1]:
|
||||||
print(f'Value must be in range [{allowed_range[0]:d}, {allowed_range[1]:d}]')
|
print(f'Value must be in range [{allowed_range[0]:d}, {allowed_range[1]:d}]')
|
||||||
|
@ -214,6 +216,8 @@ class Menu(object):
|
||||||
print(f'Value must be at least {allowed_range[0]:d}')
|
print(f'Value must be at least {allowed_range[0]:d}')
|
||||||
else:
|
else:
|
||||||
print(f'Value must be at most {allowed_range[1]:d}')
|
print(f'Value must be at most {allowed_range[1]:d}')
|
||||||
|
elif not zero_allowed and value == 0:
|
||||||
|
print("Value cannot be zero")
|
||||||
else:
|
else:
|
||||||
return value
|
return value
|
||||||
except ValueError:
|
except ValueError:
|
||||||
|
|
Loading…
Reference in New Issue