oyth94
oyth94

Reputation: 7

Creating a function without using print and input

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

Upvotes: 0

Views: 2607

Answers (3)

jwarner112
jwarner112

Reputation: 1502

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).

Upvotes: 1

progrenhard
progrenhard

Reputation: 2363

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.

Upvotes: 1

Martijn Pieters
Martijn Pieters

Reputation: 1123430

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'

Upvotes: 2

Related Questions