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 tofind_mininstead of using the pre-defined variablesxandythat the assignment requires you to keep. - You didn't retain the original variable definitions for
a,b,w,zand their correspondingmin_ab,min_wzassignments.
How to Test This Correctly
- Open your terminal/command line and launch IPython:
ipython - Run your corrected script:
You should see the same output as the original code (plus the new%run exercise.pymax_xyvalue at the end). - Verify the function and variables exist:
You'll see%whofind_min,find_max,x,y,min_xy, and all other required variables listed. - 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
相关产品推荐
相关产品推荐

