Python Forum

Full Version: Can someone help me please
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi i was wondering if someone could explain and help me Develop a Python function which either returns the float square of its parameter x if the parameter is a number, or prints the string "Sorry Dave, I'm afraid I can't do that" if the parameter is a string, and then returns 0.0.

It my lab project and im kind of confused on what its asking me to do
What have you tried? We have an aversion to doing people's homework around her, but we are happy to help you fix problems with code you have written.

You'll need a def statement to define the function, the ** operator for getting the square, and probably a try/except block to handle the conditional output.
(Oct-18-2017, 09:10 PM)ichabod801 Wrote: [ -> ]What have you tried? We have an aversion to doing people's homework around her, but we are happy to help you fix problems with code you have written.

You'll need a def statement to define the function, the ** operator for getting the square, and probably a try/except block to handle the conditional output.

hi thanks for the reply! ok so you said i need to define a function first what should i cal the function in this case? and could you break the task down for me in simpler words and like explain what its asking me to do please
thank you for the reply
Call it whatever you want to call it.  It squares numbers sometimes, so why not name it what it does: square.

You should start by mocking up what you want to make, with some test data, so you can make sure the output is what you expect for a few different inputs.  Something like
# define your function here

tests = [4, 2.6, "spam", -3, 0]
for test in test:
    # call your function here