You need to enable JavaScript to run this app.
最新活动
大模型
产品
解决方案
定价
生态与合作
支持与服务
开发者
了解我们

Python嵌套if语句实现风速分类及现有代码修正求助

Python嵌套if语句实现风速分类及现有代码修正求助

Hey there! Let's work through fixing your code step by step—you're really close, just a few small issues tripping you up right now.

First, let's spot the problems in your original code:

  • Wind speed is a string, not a number: You're getting wind_speed via input() which returns a string, so comparing it with numbers will throw an error. You need to convert it to a float/int first.
  • wind_type isn't initialized: You're trying to use += on wind_type but it doesn't exist yet—you need to define it before adding to it, or just assign it directly.
  • Indentation mistakes: The wind speed if/elif/else blocks are indented incorrectly, so they're not part of the temperature branches like you want.
  • Wrong lower() placement: You called .lower() on the prompt string instead of the user's input. It should be applied to the result of input().
  • Lots of repeated code: The rain check and wind classification logic is identical across all three temperature cases—we can clean that up to avoid redundancy.

Here's the fixed and optimized version of your code:

# Get user inputs and convert to appropriate types
temperature = float(input("Please enter the temperature in Celsius: "))
# Fix lower() placement to apply to user input
raining = input("Is it raining? Yes/No: ").lower()
# Convert wind speed to float for numerical comparisons
wind_speed = float(input("Please enter the wind speed in km/h: "))

# Classify temperature base condition
if temperature >= 25:
    condition = "Hot"
elif temperature >= 15:
    condition = "Warm"
else:
    condition = "Cold"

# Add rain status to condition
if raining == "yes":
    condition += " and raining"
else:
    condition += " and dry"

# Classify wind type (initialize wind_type first)
if 0 <= wind_speed <= 5:
    wind_type = "Calm"
elif 6 <= wind_speed <= 49:
    wind_type = "Breezy"
elif 50 <= wind_speed <= 61:  # Enforce the range you noted in comments
    wind_type = "Windy"
else:
    # Fallback for speeds outside your defined range
    wind_type = "Unknown wind condition"

# Final output
print(f"The weather is {condition} and {wind_type}")

If you still want to keep the wind classification nested inside temperature branches, here's that adjusted version:

temperature = float(input("Please enter the temperature in Celsius: "))
raining = input("Is it raining? Yes/No: ").lower()
wind_speed = float(input("Please enter the wind speed in km/h: "))

# Initialize variables upfront
condition = ""
wind_type = ""

if temperature >= 25:
    condition = "Hot"
    if raining == "yes":
        condition += " and raining"
    else:
        condition += " and dry"
    # Fix indentation and assign wind_type directly
    if 0 <= wind_speed <= 5:
        wind_type = "Calm"
    elif 6 <= wind_speed <= 49:
        wind_type = "Breezy"
    elif 50 <= wind_speed <= 61:
        wind_type = "Windy"
    else:
        wind_type = "Unknown wind condition"

elif temperature >= 15:
    condition = "Warm"
    if raining == "yes":
        condition += " and raining"
    else:
        condition += " and dry"
    if 0 <= wind_speed <= 5:
        wind_type = "Calm"
    elif 6 <= wind_speed <= 49:
        wind_type = "Breezy"
    elif 50 <= wind_speed <= 61:
        wind_type = "Windy"
    else:
        wind_type = "Unknown wind condition"

else:
    condition = "Cold"
    if raining == "yes":
        condition += " and raining"
    else:
        condition += " and dry"
    if 0 <= wind_speed <= 5:
        wind_type = "Calm"
    elif 6 <= wind_speed <= 49:
        wind_type = "Breezy"
    elif 50 <= wind_speed <= 61:
        wind_type = "Windy"
    else:
        wind_type = "Unknown wind condition"

print(f"The weather is {condition} and {wind_type}")

A bonus optimization: Use a function to avoid repeated code

If you want to make your code even cleaner, move the wind classification into a reusable function. This way, if you ever need to adjust wind ranges, you only change it once:

def classify_wind(speed):
    if 0 <= speed <= 5:
        return "Calm"
    elif 6 <= speed <= 49:
        return "Breezy"
    elif 50 <= speed <= 61:
        return "Windy"
    else:
        return "Unknown wind condition"

# Main code
temperature = float(input("Please enter the temperature in Celsius: "))
raining = input("Is it raining? Yes/No: ").lower()
wind_speed = float(input("Please enter the wind speed in km/h: "))

# Classify temperature and rain status
if temperature >= 25:
    condition = "Hot"
elif temperature >= 15:
    condition = "Warm"
else:
    condition = "Cold"

# Shorten rain check with ternary operator
condition += " and raining" if raining == "yes" else " and dry"

# Use the function to get wind type
wind_type = classify_wind(wind_speed)

print(f"The weather is {condition} and {wind_type}")

This should work perfectly now! Let me know if you have any questions about any of the changes 😊

火山引擎 最新活动