Python 3中ValueError问题:租赁店库存追踪程序输入校验报错
Let's break down what's causing your ValueError and fix it step by step:
The Root Cause
Your error happens because when the number_of_items_hired_entry is empty, you're trying to convert an empty string '' to an integer with int(number_of_items_hired_entry.get())—and Python can't do that, hence the ValueError: invalid literal for int() with base 10: ''.
On top of that, your last condition has a logical flaw:
if int(number_of_items_hired_entry.get ()) <0 and int(number_of_items_hired_entry.get ()) >500:
No number can be both less than 0 and greater than 500 at the same time—you need to use or instead of and here. Also, you're repeating the same get() and int() calls multiple times, which is inefficient and error-prone.
Fixed Code
Here's the revised version of your check_entries() function with all issues addressed:
def check_entries(): # This function checks whether what the user entered is correct for the entry and store global customer_name_error_label global receipt_numb_error_label global item_hired_name_error_label global numb_items_hired_error_label global numb_items_hired_numb_rel_error_label # Clear all existing error labels first (cleaner than checking each later) for label in [customer_name_error_label, receipt_numb_error_label, item_hired_name_error_label, numb_items_hired_error_label, numb_items_hired_numb_rel_error_label]: if label: label.grid_forget() # Get all input values once to avoid repeated calls customer_name = customer_name_entry.get().strip() receipt_number = receipt_number_entry.get().strip() item_hired = item_hired_entry.get().strip() num_items_str = number_of_items_hired_entry.get().strip() is_valid = True # Validate customer name (note: isalpha() fails for names with spaces, e.g., "John Doe" # If you want to allow spaces, use a custom check like all(c.isalpha() or c.isspace() for c in customer_name) if not customer_name or not customer_name.isalpha(): customer_name_error_label = Label(main_frame, text="Please enter a name using only letters (no numbers), and ensure it's not empty.", bg='red') customer_name_error_label.grid(row=0, column=3) is_valid = False # Validate receipt number if not receipt_number or not receipt_number.isdigit(): receipt_numb_error_label = Label(main_frame, text="Please enter a receipt number using only digits (no letters/symbols), and ensure it's not empty.", bg='red') receipt_numb_error_label.grid(row=1, column=3) is_valid = False # Validate item name (same note about spaces/special chars if needed) if not item_hired or not item_hired.isalpha(): item_hired_name_error_label = Label(main_frame, text="Please enter an item name using only letters (no numbers/symbols), and ensure it's not empty.", bg='red') item_hired_name_error_label.grid(row=2, column=3) is_valid = False # Validate number of items if not num_items_str or not num_items_str.isdigit(): numb_items_hired_error_label = Label(main_frame, text="The number of items hired must be a digit (no letters/symbols), and cannot be empty.", bg='red') numb_items_hired_error_label.grid(row=4, column=3) is_valid = False else: num_items = int(num_items_str) if num_items <= 0 or num_items >= 500: numb_items_hired_numb_rel_error_label = Label(main_frame, text="The number of items hired must be greater than 0 and less than 500.", bg='red') numb_items_hired_numb_rel_error_label.grid(row=5, column=2) is_valid = False # If all validations pass, proceed if is_valid: append_lists()
Key Improvements
- Avoids empty string conversion: We first check if
num_items_stris non-empty and a digit before converting toint(). - Fixes logical condition: Changed
andtoorfor the number range check, and adjusted the range to<=0 or >=500to catch invalid values. - Reduces redundant code: Fetched all input values once, and cleared error labels in a loop instead of repeating
grid_forget()for each. - Better empty input handling: Added checks for empty strings (using
not customer_nameetc.) to catch cases where the user just hits enter without typing anything. - Note on name validation: The original
isalpha()will fail for names with spaces (like "Mary Ann"). If you want to allow spaces, replace the check with:if not customer_name or not all(c.isalpha() or c.isspace() for c in customer_name):
内容的提问来源于stack exchange,提问作者Vikash Patel

