Python 3.8.2猜数字程序无法猜对数字问题求助
Hey there! Let's break down what's going on with your number guessing game and fix it step by step.
What's Causing the Confusing Hints?
First off, let's clear up one thing: random.randint(0, num) only generates whole integers (so for a max of 5, it picks 0,1,2,3,4,5—no decimals like 3.5). Your weird conflicting hints (guessing 3 says "More!" and 4 says "Less!") are 100% due to mixed-up logic in your hint messages, not the random number generator.
Look at your second guess block:
if answer < r: print('Lower!') # WRONG! If your guess is smaller than the secret number, you need to guess HIGHER, not lower! if answer > r: print('Higher!') # WRONG! If your guess is bigger than the secret number, you need to guess LOWER, not higher!
This is the exact opposite of what you want. For example, if the secret number is 4:
- You guess 3 (too small) → your code incorrectly says "Lower!" instead of "More!"
- You guess 5 (too big) → it incorrectly says "Higher!" instead of "Less!"
On top of that, your code has a lot of redundant checks (repeating if answer != r multiple times) which makes it easier to make these kinds of mistakes.
Fixing the Issue
We'll fix two things:
- Correct the hint logic so it matches what the user needs to do next.
- Simplify the code with a loop to avoid redundant checks and make it easier to read.
Simplified & Corrected Code
import random # Get the user's desired maximum number max_number = int(input("What is the highest number you want? ")) # Generate a secret integer between 0 and max_number (inclusive) secret_number = random.randint(0, max_number) # Give the user 3 guess attempts for attempt in range(1, 4): # Ask for guess with context based on attempt number if attempt == 1: user_guess = int(input("Guess the number! ")) elif attempt == 2: user_guess = int(input("Second guess out of 3! ")) else: user_guess = int(input("Final guess! ")) # Check if the guess is correct if user_guess == secret_number: if attempt == 1: print("Correct!") elif attempt == 2: print("Correct!") else: print("Finally!") quit() # Give correct hint if guess is wrong else: if user_guess < secret_number: print("Higher!") # Guess was too small—need to go up else: print("Lower!") # Guess was too big—need to go down # If all 3 attempts are used and no correct guess print('Damn... you lost.')
How This Works (For Beginners)
- Random Number Clarity:
random.randint(a, b)always returns an integer betweenaandb(including both ends). So no weird decimal numbers here—your original confusion about the random module was unfounded! - Loop for Attempts: Using
for attempt in range(1,4)gives us a clean way to handle 3 guesses without repeating code. Each iteration corresponds to one guess attempt. - Consistent Hints: We now have one set of hint logic that works for all attempts:
- If guess < secret number → tell user to guess higher
- If guess > secret number → tell user to guess lower
- Readable Variable Names: Renamed vague variables like
rtosecret_numberandnumtomax_numberso you can easily tell what each variable does.
Test It Out!
If you set max number to 5 and the secret number is 4:
- Guess 3 → gets "Higher!" (correct hint)
- Guess 4 next → gets "Correct!"
No more conflicting messages!
内容的提问来源于stack exchange,提问作者Daanu

