0

I am creating a function useless with 3 parameters and the output is 'That was a waste of my time' The code that I tried is:

def useless("Hello, 95, "jello"):
   x = 'That was a waste of my time'
   return x

There is supposed to be an output but there is none... and it also says I am making a syntax error on the first line.

The output is supposed to be 'This was a waste of my time'

I can't detect anything that is wrong... What can I do?? I am not getting an output and I am also having a syntax error

3 Answers 3

2

You are not getting any output because you have a syntax error.

Your function parameters are not valid; they are a) not valid Python like that, and b) incomplete:

def useless("Hello, 95, "jello"):

The "Hello string is missing a closing parenthesis, but you cannot just put literal values in a function header anyway. Remove everything between the parenthesis:

def useless():
    x = 'That was a waste of my time'
    return x

The rest of your function is fine:

>>> def useless():
...     x = 'That was a waste of my time'
...     return x
... 
>>> useless()
'That was a waste of my time'
Sign up to request clarification or add additional context in comments.

Comments

1

First, you missed the closing " in useless that would end at Hello. Python now thinks you want to include "Hello, 95, " and at jello it gets confused as hell, and so-on.

Also, you seem to have a fundamental misunderstanding of how functions use parameters and arguments. First, let's define the difference between the two.
Parameters are what you put inside a function when you define it:

def Function(parameter_1,parameter_2):
    a = parameter_1
    b = parameter_2

Arguments meanwhile are the values you pass into the functions when you call them:

Function(argument_1,argument_2)

Thinking in mathematical terms, when you call a function where X=? and Y=?, you are saying:

"call Function, and plug in "value" for x and "value2" for y"

Running with these examples, you can rewrite your function useless in two ways:
You can do no arguments, where it will always return "x":

def useless():
    x='That was a waste of my time'
    return x

Or, you can pass arguments through parameters in the function to give more flexibility to your function:

def useless(x):
    return x

useless('That was a waste of my time')

Either will return the same line.

Finally, to ensure you know the difference between return and print; When using a function, you don't always want to print your result, but you still want to get it. That is where return works; Calling the function will then bring back whatever value that you tell it do with the return line.

For example, in the first useless I defined with no parameters, the function takes no argument and returns x as it is defined, always.

The other useless I defined, however, is essentially a "print" function because it takes the argument you give it and just returns it automatically.

Why return in these cases and not print? Because now that you're returning, you have the option of telling Python to print or store useless!
Thanks to return, you can either write:

print useless(x)

or

variable = useless(x)

where now you can use variable however you like, even if you just do print variable.

I hope my over-explaining hasn't overwhelmed. If you still don't understand, try doing the Codecademy Python tutorial, which may help you to conceptually grasp all of this (the website isn't completely behaving at the moment, but if you do the lesson in Codecademy Labs, you can still hack your way through the tutorials).

4 Comments

Thank you! That helped alot. For Boolean though, If I were to use Boolean would I do : variable1 = True ? Is that how it works? (havnt learned Boolean yet....)
so it becomes for example: def useless(Hello, value, jello): (define parameters like Hello = ..., value = ..., jello = ...) x = 'That was a waste of time' return x
For booleans, you can either do True or False (or !True or !False, you see?)... But for your second comment, can you please reformat it with the code tag so I can understand it? :)
Also, if you thought my answer was best, please select it as your answer. When you do that, it awards me points for my effort. Likewise with anybody else! :)
1
def useless("Hello, 95, "jello"):
   x = 'That was a waste of my time'
   return x

should be...

def useless(Hello, value, jello):
   x = 'That was a waste of my time'
   return x

you can't have a string or int's as a variable name. You are declaring variables when you are setting args.

also just def useless() with no args is better in your scenario.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.