You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何实现随机数奇偶猜题直至答对,及修复输入反馈逻辑异常

Build & Fix the Odd/Even Guessing Game

Let's start by building the game from scratch, then address the bugs in your current code based on what you described.

Step-by-Step Guide to Write the Correct Code

We'll use Python here since it's straightforward for this kind of task:

  1. Import the random module: To generate our target random number
  2. Generate the target number and determine its parity: Figure out if it's odd or even
  3. Loop for user input: Keep asking until they guess correctly, with feedback on wrong attempts

Here's the full working code:

import random

# Generate a random integer between 1 and 100 (adjust range as needed)
target_num = random.randint(1, 100)
# Determine if the number is odd or even
target_parity = "奇数" if target_num % 2 != 0 else "偶数"

print("我生成了一个1-100之间的随机数,请猜测它是奇数还是偶数!")

while True:
    user_guess = input("请输入你的猜测(奇数/偶数):").strip()
    
    # Validate input first
    if user_guess not in ["奇数", "偶数"]:
        print("请输入有效的选项:奇数或偶数!")
        continue
    
    # Check if the guess is correct
    if user_guess == target_parity:
        print(f"Congratulations!你猜对了!这个数是{target_num},确实是{target_parity}。")
        break
    else:
        print("猜错啦,请再试一次!")

Fixing Your Existing Code's Issues

From your description, your current code has two main problems:

  • It checks if the user's input number matches the target number (instead of checking parity)
  • It doesn't loop to prompt the user again after a wrong guess

What Your Broken Code Might Look Like

Here's a rough example of the flawed code you're working with:

import random

target_num = random.randint(1, 100)
user_input = int(input("猜数字:"))

if user_input == target_num:
    print("congratulations")
# No handling for wrong guesses or invalid input, no loop

The Fixed Version

We'll adjust the logic to focus on parity and add a loop for repeated attempts:

import random

target_num = random.randint(1, 100)
target_parity = "奇数" if target_num % 2 != 0 else "偶数"

print("我生成了一个1-100之间的随机数,请猜测它是奇数还是偶数!")

while True:
    try:
        user_input = input("请输入你的猜测(奇数/偶数,或者直接输入数字):").strip()
        
        # Handle if user inputs a number instead of text
        if user_input.isdigit():
            user_num = int(user_input)
            user_parity = "奇数" if user_num % 2 != 0 else "偶数"
        else:
            # Validate text input
            if user_input not in ["奇数", "偶数"]:
                print("输入无效,请输入“奇数”、“偶数”或者一个数字!")
                continue
            user_parity = user_input
        
        # Check parity match
        if user_parity == target_parity:
            print(f"congratulations!你猜对了!这个数是{target_num},确实是{target_parity}。")
            break
        else:
            print("猜错啦,请再试一次!")
    except ValueError:
        print("输入无效,请输入有效的内容!")

Key Fixes Explained

  • Added a while True loop to keep the game running until the user guesses correctly
  • Changed the core logic from checking number equality to checking parity (odd/even match)
  • Added input validation to handle invalid entries and guide the user
  • Added clear feedback for wrong guesses to prompt another attempt

内容的提问来源于stack exchange,提问作者Morty Smith

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.19 03:41:59