From 33a9f12ac026aedf0ce73c27d07150d6cc83dee0 Mon Sep 17 00:00:00 2001 From: Robert Maikher Date: Thu, 30 Aug 2018 15:12:11 +0200 Subject: [PATCH] Parameter to disallow zero in input_int --- text_interface/editing.py | 8 +++++--- text_interface/helpermenus.py | 6 +++++- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/text_interface/editing.py b/text_interface/editing.py index 599ba3f..3d99891 100644 --- a/text_interface/editing.py +++ b/text_interface/editing.py @@ -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? - print(f'You added {add_stock:d} to the stock of {product}') + 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 diff --git a/text_interface/helpermenus.py b/text_interface/helpermenus.py index 80ad6b4..795cf93 100644 --- a/text_interface/helpermenus.py +++ b/text_interface/helpermenus.py @@ -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: