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

Python简单函数定义与调用的作业问题求助

Hey Marie, let's get your Python assignment sorted out—your find_min function logic is actually correct, the issue is in how you're calling it and structuring the full file to meet the assignment requirements. Let's break this down step by step.

Fixed exercise.py Code

Here's the complete, corrected code that meets all the assignment requirements (including the find_max addition):

# Define the find_min function first as required
def find_min(x, y):
    if x < y:
        return x
    else:
        return y

# Define the find_max function (second part of the assignment)
def find_max(x, y):
    if x > y:
        return x
    else:
        return y

# Keep all original variable definitions from the initial code
x = 3
y = 4
a = 12.3
b = 13.7
w = -3.9
z = -4.7

# Refactor to use find_min with existing variables (this fixes your test failures)
min_xy = find_min(x, y)
print(min_xy)

min_ab = find_min(a, b)
print(min_ab)

min_wz = find_min(w, z)
print(min_wz)

# Call find_max to get the larger value of x and y
max_xy = find_max(x, y)
print(max_xy)

Why Your Original Code Failed the Tests

The test errors you saw were because:

  • You were passing literal values (3, 4) directly to find_min instead of using the pre-defined variables x and y that the assignment requires you to keep.
  • You didn't retain the original variable definitions for a, b, w, z and their corresponding min_ab, min_wz assignments.

How to Test This Correctly

  1. Open your terminal/command line and launch IPython:
    ipython
    
  2. Run your corrected script:
    %run exercise.py
    
    You should see the same output as the original code (plus the new max_xy value at the end).
  3. Verify the function and variables exist:
    %who
    
    You'll see find_min, find_max, x, y, min_xy, and all other required variables listed.
  4. Test the functions interactively:
    find_min(5, 2)  # Should return 2
    find_max(-10, -5)  # Should return -5
    

This should resolve all your test failures and meet every part of the assignment requirements.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.04.30 09:32:42