Python新手技术咨询:如何将输入数字转换为时间格式并添加到字典中
Fixing Your Python Time Conversion & Dictionary Storage Code
Hey there! Let's fix up your code so it does exactly what you need: converting those 4-digit time inputs to a readable time format, and storing everything correctly in the dictionary.
First, let's address the two key issues in your original code:
- Calling strings like functions: Lines like
employee1_data[time] = time()are wrong—timeis a string from user input, not a function. This would throw aTypeErrorwhen you run the code. - Missing time format conversion: You haven't added logic to turn inputs like "0400" into "4:00" or "0352" into "3:52".
Here's the corrected, working code:
employee1_data = {} def login_time_admin_a(): while True: print("============================== | PLEASE ENTER YOUR INFO | ==============================") time_input = input("ENTER TIME (4 digits, e.g., 0400): ") # Validate time input: must be 4 digits, no letters if not time_input.isdigit() or len(time_input) != 4: print("====================== | INCORRECT INPUT: Enter 4-digit numbers only | ======================") continue # Convert 4-digit string to time format (e.g., "0400" → "4:00", "0352" → "3:52") hours = time_input[:2].lstrip('0') # Remove leading zero from hours # Handle edge case where hours are "00" (strip leaves empty string) hours = hours if hours else "0" minutes = time_input[2:] formatted_time = f"{hours}:{minutes}" date_input = input("ENTER DATE (e.g., 2024-05-20): ") # Basic date validation (expand with datetime module if you need stricter checks) if not date_input.replace('-', '').isdigit(): print("====================== | INCORRECT INPUT: Enter a valid date (e.g., 2024-05-20) | ======================") continue print("============================ | THANK YOU FOR LOGGING IN | ============================") # Store formatted time and date in the dictionary with clear, consistent keys employee1_data["login_time"] = formatted_time employee1_data["login_date"] = date_input break # Exit loop after successful input # Test the function login_time_admin_a() # Verify the stored data print("Stored Employee Data:", employee1_data)
Key improvements explained:
- Stricter input validation: We now ensure the time input is exactly 4 digits (no letters, no wrong length) and add basic checks for date format.
- Time conversion logic:
- Grabs the first two characters (hours) and strips leading zeros to turn "04" into "4".
- Keeps the last two characters (minutes) as-is to preserve values like "00" or "52".
- Handles edge cases like "0000" by setting hours to "0" if stripping zeros leaves an empty string.
- Fixed dictionary storage: We use clear, fixed keys (
"login_time"and"login_date") instead of raw input values (which would overwrite data if the same time/date is entered twice). No more trying to call strings as functions!
Example Output:
If you enter 0400 as time and 2024-05-20 as date, the dictionary will output:
{'login_time': '4:00', 'login_date': '2024-05-20'}
Entering 0352 as time will store '3:52' in the dictionary instead.
内容的提问来源于stack exchange,提问作者Jeon Lei
相关产品推荐
相关产品推荐

