Python实现1-100不重复数字输入并升序排序的程序问题排查
Fixing Your Python Sorting Program Issues
Let's break down the problems in your code and fix them step by step:
Key Issues in the Original Code
- Repeated List Printing: You’re looping through every element in
arrayand printing the sorted list each time the validation condition passes. That’s why you see the sorted list multiple times instead of once. - Broken Validation Logic: The condition
if i > 100 or i < 1 or aa and bb and cc and dd and ee and ff > 1doesn’t work as intended. Checking individual element counts like this only flags cases where every element is repeated (not any duplicate), and the logical operator grouping is incorrect. - Vague Error Messaging: The error message lumps two distinct issues (invalid range and duplicates) together without clarifying which problem occurred, and the validation doesn’t properly separate these checks.
Fixed Code (Cleaner Input Handling)
# Collect 6 valid numbers from the user numbers = [] while len(numbers) < 6: try: num = int(input(f"Enter number {len(numbers)+1}: ")) # Validate number range if not (1 <= num <= 100): print("Error: Number must be between 1 and 100 (inclusive).") continue # Check for duplicates if num in numbers: print("Error: Numbers cannot be repeated.") continue numbers.append(num) except ValueError: print("Error: Please enter a valid whole number.") # Sort and print the final result once numbers.sort() print("Sorted numbers in ascending order:", numbers)
What We Changed
- Streamlined Input Collection: Instead of creating separate variables for each input, we use a loop to gather exactly 6 valid numbers. This makes the code shorter and easier to update if you ever need more/less numbers.
- Granular Validation:
- Added
try/exceptto catch non-integer inputs (a gap in your original code). - Separated range checks and duplicate checks with specific error messages for each issue.
- Added
- Single Print for Sorted List: We only sort and print the list once after all valid inputs are collected, eliminating repeated output.
- Simpler Duplicate Detection: Using
num in numberslets us catch duplicates as we collect each input, which is more efficient than counting every element later.
Alternative Fixed Version (Matching Your Original Input Structure)
If you prefer to keep your original separate input variables, here’s a fixed version that addresses the core issues:
# User input section try: a = int(input("Enter the first number: ")) b = int(input("Enter the second number: ")) c = int(input("Enter the third number: ")) d = int(input("Enter the fourth number: ")) e = int(input("Enter the fifth number: ")) f = int(input("Enter the sixth number: ")) except ValueError: print("Error: Please enter valid integers only.") exit() array = [a, b, c, d, e, f] is_valid = True # Check all numbers are within 1-100 for num in array: if not (1 <= num <= 100): print("Error: All numbers must be between 1 and 100 (inclusive).") is_valid = False break # Check for duplicates if range is valid if is_valid: if len(set(array)) != len(array): print("Error: Numbers cannot be repeated.") is_valid = False # Sort and print only if all checks pass if is_valid: array.sort() print("Sorted numbers:", array)
内容的提问来源于stack exchange,提问作者hmood
相关产品推荐
相关产品推荐

