为何我的Python代码未遵循if语句条件,导致终止循环的负数被当作最小值输出
Let's break down what's going on with your code, and why you're seeing that unexpected (negative?) value for mini. First, let's clear up: your if x > 0 statement is working correctly—negative inputs aren't being processed in that block. The real issues lie in your initial variable setup and the order of operations in your loop.
1. Your mini starts at the wrong value
You initialized mini = 0, but 0 isn't a positive number (it's neither positive nor negative). So when you input positive numbers (all greater than 0), min(x, mini) will always return 0 instead of the smallest positive number you entered. For example, if you input 5, then 3, mini stays 0 instead of updating to 3.
Wait, you mentioned mini outputs the negative number you used to terminate the loop—with your current code, that shouldn't happen. Unless you made a typo (like writing if x >= 0 instead of if x > 0), negative inputs never touch the mini = min(x, mini) line. Maybe you confused 0 with a negative number? Or perhaps you tested by entering a negative number first (before any positives)—in that case, mini stays 0, which isn't negative, but it's still wrong.
2. Your loop order is causing extra counts and incorrect state
Your loop first increments counter before getting the user's input. That means:
- Even if you enter a negative number to terminate,
counterstill goes up by 1 (skewing your average calculation if you didn't enter any positives). - The initial
x = 0triggers the loop immediately, socounterstarts at 1 before you even enter your first number.
How to fix this
Let's rewrite the code to fix these issues:
# Avoid using 'sum' as a variable name (it's a built-in Python function) total = 0 counter = 0 maximum = float('-inf') # Start with the smallest possible value minimum = float('inf') # Start with the largest possible value while True: x = int(input("Enter a positive number (enter a negative to stop): ")) # Terminate loop if negative is entered if x < 0: break # Skip non-positive numbers (like 0) without counting them if x <= 0: print("That's not a positive number—try again!") continue # Update values only for valid positive inputs counter += 1 total += x maximum = max(x, maximum) minimum = min(x, minimum) # Only print results if we got at least one positive number if counter > 0: average = total / counter print(f"Maximum: {maximum}") print(f"Total sum: {total}") print(f"Average: {average}") print(f"Minimum positive number: {minimum}") else: print("No positive numbers were entered.")
Key fixes explained:
- We initialize
minimumtofloat('inf')(infinity), so the first valid positive number will automatically become the new minimum. Every subsequent positive number will compare against this and update it if smaller. - We initialize
maximumtofloat('-inf')so the first positive number becomes the maximum, which makes sense for your use case. - The loop now gets input first, checks for termination, skips invalid inputs (like 0), and only updates counters/values when it gets a valid positive number.
- We add a check to avoid division by zero if the user enters a negative number right away.
So to answer your original question: your if statement was following its condition—negative inputs weren't being processed. The weird output for mini came from your initial value choice and loop order, not a failure in the if logic.
内容的提问来源于stack exchange,提问作者Davey Jones

