Click here to Skip to main content
16,022,060 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
the question is this:
Open the max.py file you have in this directory.
You will see two functions: max and main.

In max, the algorithm is written as comments, but there is no code.
You should translate this algorithm to code.

In main, there are three print statements which call max and print
its result. These would let you check if max is working right.

There is also a comment asking you to write one more print statement
with a call to max. In that comment, it asks you to take the max
of two numbers and print them out.
Add a line of code to do this.

What I have tried:

I wrote this code and it works:
# max function to return the larger of two numbers
def max(a, b):
    if a > b:
        return a
    else:
        return b

def main():
    # Printing in the expected format:
    print(f"max(42, -69) is {max(42, -69)}")  # Expected: max(42, -69) is 42
    print(f"max(33, 0) is {max(33, 0)}")      # Expected: max(33, 0) is 33
    print(f"max(0.123, 0.223) is {max(0.123, 0.223)}")  # Expected: max(0.123, 0.223) is 0.223
    print(f"max(-0.123, -0.223) is {max(-0.123, -0.223)}")  # Expected: max(-0.123, -0.223) is -0.123

    # Your custom test case:
    print(f"max(-999, 123) is {max(-999, 123)}")  # Expected: max(-999, 123) is 123

if __name__ == "__main__":
    main()


but i get this error:
max(-999, 123) is 123					      <
Your file did not match the expected output
Testing max(-999, -2147483648) ...
							      >	max(42, -69) is 42
							      >	max(33, 0) is 33
							      >	max(0.123, 0.223) is 0.223
							      >	max(-0.123, -0.223) is -0.123
Your file did not match the expected output
Testing max(-999, 123) ...
							      >	max(42, -69) is 42
							      >	max(33, 0) is 33
							      >	max(0.123, 0.223) is 0.223
							      >	max(-0.123, -0.223) is -0.123
Your file did not match the expected output
Testing max(-999, 567) ...
Posted

1 solution

I'd guess that it's an indentation problem: you added the line
Python
print(f"max(-999, 123) is {max(-999, 123)}")  # Expected: max(-999, 123) is 123
So I'd suspect that you used a tab instead of spaces, or spaces instead of a tab. Although they look the same on screen, they don't look the same to the interpreter and since indentation is significant in Python (one reason why it's a poor language for beginners) it can have a massive effect on execution.

You probably need to talk to your teacher: it's his app that is checking your code and we don't have access to that, but I'd start with the debugger (pdb — The Python Debugger — Python 3.13.0 documentation[^]) and step through the whole app looking closely at what is happening.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900