Parameter to disallow zero in input_int

This commit is contained in:
Robert Maikher 2018-08-30 15:12:11 +02:00
parent ac3ab8099b
commit 33a9f12ac0
2 changed files with 10 additions and 4 deletions

View File

@ -129,9 +129,11 @@ class AdjustStockMenu(Menu):
print(f'The stock of this product is: {product.stock:d}')
print('Write the number of products you have added to the stock')
print('Alternatively, correct the stock for any mistakes')
add_stock = self.input_int('Added stock', allowed_range=(-1000, 1000))
# TODO: Print something else when adding negative stock?
add_stock = self.input_int('Added stock', allowed_range=(-1000, 1000), zero_allowed=False)
if add_stock > 0:
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

View File

@ -200,13 +200,15 @@ class Menu(object):
def invalid_menu_choice(self, in_str):
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:
result = self.input_str(prompt, end_prompt, default=default)
if result == '' and null_allowed:
return False
try:
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 allowed_range[1]:
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}')
else:
print(f'Value must be at most {allowed_range[1]:d}')
elif not zero_allowed and value == 0:
print("Value cannot be zero")
else:
return value
except ValueError: