Re: [Tutor] function question

2019-01-05 Thread Steven D'Aprano
Hello David, and welcome!

On Sat, Jan 05, 2019 at 11:18:04AM -0500, David Lynch wrote:

[...]
> From what I've read about functions I should be able to define a function
> with 2 variables? And then I can add all of my code into that function by
> indenting it. 

So far so good! Here's an example of such a function:


def add(x, y):
return x + y


Of course in real programs we wouldn't bother with such a trivial 
function, since it is easier to read and write "a + b" rather than 
"add(a, b)". But it illustrates the concept.


> And then shouldn't I be able to
> function(float(input("enter input: ")))
> function(float(input("enter input 2: ")))
> return(function)
> ??

No, nothing like this. You are calling the function twice, but each time 
only supplying a single argument when it needs two. And the "return" 
keyword is only legal inside functions.

I *strongly* recommend that you make use of one of Python's best and 
most powerful features: the interactive interpreter. There you can try 
out small snippets of code with ease, see the result of running code 
live, use Python as a calculator, etc. That's an excellent way of 
learning to use the language, and you can always ask for help here if 
you have questions.

Do you know how to start the interactive interpreter? That will depend 
on your operating system (Windows, Mac, Linux) but generally you will 
open a "terminal" or "console", which gives you a command-line. That 
will have a % or $ prompt where you type commands.

Type "python" and hit the ENTER key. You will then see something similar 
to this:

  Python 3.5.2 (default, Oct 12 2016, 10:47:40)
  [GCC 4.1.2 20080704 (Red Hat 4.1.2-55)] on linux
  Type "help", "copyright", "credits" or "license" for more information.

and then be presented with >>> the default Python prompt, where you type 
Python code. In my examples below, I use the custom prompt "py>" because 
I think it looks nicer :-)

If you can get to the point of seeing the Python prompt, you're ready to 
start running live Python code. At the prompt, type:

12*23

and hit ENTER, and you should see the answer 276:

py> 12*23
276


Now type in your function. Notice that you need to indent the body of 
the function, and that the prompt changes to ... inside the body.


py> def add(x, y):
... return x + y
...
py>


(Remember, don't type the "py>" or "..." prompts.)

Now you should be ready to try calling the function:


py> add(99, 7)
106


If you get to that point, then you're ready to go to the next stage: 
intentionally doing something wrong so you can see what errors to 
expect.

What happens if you enter:

add(99)

at the prompt? What error do you get?

Once you've understood that, they you can start writing more serious, 
useful functions.

Good luck, and don't hesitate to ask if you have any further questions!



-- 
Steve
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] function question

2019-01-05 Thread Alan Gauld via Tutor
On 05/01/2019 16:18, David Lynch wrote:
> Hello,
> I'm not sure if this is where I can find this sort of help but I am
> struggling understanding functions. I have written a simple math code but I
> feel like it could be improved if I were to use it in a function.

You arein the right place and you have the right idea.

You might find my tutorial on functions worth looking at:

http://www.alan-g.me.uk/l2p2/tutfunc.htm

But for the short version keep reading...


> From what I've read about functions I should be able to define a function
> with 2 variables? And then I can add all of my code into that function by
> indenting it.

That's correct, more or less.

So a function with two variables will look like

def aFunction(var1,var2):
 your code here
 return result

>  And then shouldn't I be able to
> function(float(input("enter input: ")))

No because you are only passing one variable into the
function - the result of the input)() function.
input() is just a function like any other, it
takes an input parameter and returns a value.


> function(float(input("enter input 2: ")))

And again you call your function with a single value.
The function has no memory of previous calls so it
won't know about your previous attempt with input

> return(function)

And the return statement needs to be inside the
function. It returns the resuilt to the caller
of the function.

So you really want something like

result = function(input('Value1'), input('Value 2'))

Although we generally consider using input like that
a bad idea. It would be better to write:

val1 = input('Value 1')
val2 = input('value 2')
result = function(val1,val2)

It's slightly longer but much easier to debug if
things go wrong!

> Also, since it'll be in a function, when I type return(function) will it
> rerun the code from the top?

Every time you type function() it will run the
function code afresh.

To try to clarify things, here is a very simple
function that simply adds two numbers and the
code that uses it.

def add(val1,val2):
total = val1 + val2
return total

x = int( input('Val1? ') )  # convert input string to integer
y = int( input('Val2? ') )
result = add(x,y)
print(x, '+', y, '=', result) #or print("%d + %d = %d" % (x,y,result))

See my tutorial paqe for more examples and a more
complete explanation.

-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos


___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


[Tutor] function question

2019-01-05 Thread David Lynch
Hello,
I'm not sure if this is where I can find this sort of help but I am
struggling understanding functions. I have written a simple math code but I
feel like it could be improved if I were to use it in a function.

>From what I've read about functions I should be able to define a function
with 2 variables? And then I can add all of my code into that function by
indenting it. And then shouldn't I be able to
function(float(input("enter input: ")))
function(float(input("enter input 2: ")))
return(function)
??

Also, since it'll be in a function, when I type return(function) will it
rerun the code from the top?

I'd appreciate any advice!
David Lynch
print("Passive income from incentivised onboarding")
capital = float(input("Amount of capital raised: "))
amount_invested = float(input("Amount invested: "))
revenue = (capital * .75) / .2
total_royalty = (capital / 100) + 300
royalty_blocs_owned = amount_invested / 100
percentage_owned = royalty_blocs_owned / total_royalty
total_percent_owned = royalty_blocs_owned / total_royalty
passive_income = revenue * .1 * total_percent_owned
print("Assuming 75% of the capital raised is used for incentivised onboarding 
and there is no organic growth. Your passive income, by the time the onboarding 
funds are depleted, will be: ")
print(passive_income)
print("")
print("Passive income with estimated revenue")
capital = float(input("Amount of capital raised: "))
total_royalty = (capital / 100) + 300
amount_invested = float(input("Amount invested: "))
royalty_blocs_owned = amount_invested / 100
percentage_owned = royalty_blocs_owned / total_royalty
total_percent_owned = royalty_blocs_owned / total_royalty
revenue = float(input("Companies Revenue: "))
passive_income = revenue * .1 * total_percent_owned
print("Your passive income based on the revenue provided will be: ")
print(passive_income)
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Function question

2017-04-01 Thread Peter O'Doherty



On 25-03-17 11:17, Alan Gauld via Tutor wrote:


method:

print(' '.join(anotherFunction(4))


Many thanks!
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Function question

2017-04-01 Thread Peter O'Doherty

Many thanks!

On 25-03-17 11:17, Alan Gauld via Tutor wrote:

On 25/03/17 10:01, Peter O'Doherty wrote:


def myFunc(num):
  for i in range(num):
  print(i)

print(myFunc(4))
0
1
2
3
None #why None here?

Because your function does not have an explicit return
value so Python returns its default value - None.
So the print() inside the function body prints the 0-3
values then the function terminates and returns the (default)
None to your top level print.


def myFunc(num):
  for i in range(num):
  return i

print(myFunc(4))
0 #why just 0?

Because return always returns from the function immediately.
So you call the function, it enters the loop, sees the return for the
first element and exits. The print() then prints that returned value.

The preferred method to do what I think you were expecting is to build a
list:

def anotherFunction(num):
 result = []
 for i in range(num):
result.append(i)
 return result

Which is more concisely written using a list comprehension but
that would hide the general point - that you should accumulate results
in a collection if you want to return more than a single value.

To print the result you would typically use the string.join()
method:

print(' '.join(anotherFunction(4))





//=
// Peter O'Doherty

// http://www.peterodoherty.net
// m...@peterodoherty.net
//=

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Function question

2017-03-25 Thread Sri Kavi
On Sat, Mar 25, 2017 at 3:31 PM, Peter O'Doherty wrote:

>
> def myFunc(num):
> for i in range(num):
> print(i)
>
> print(myFunc(4))
> 0
> 1
> 2
> 3
> None #why None here?
>
> Because there are two print() functions, one inside the function and
another outside.  When a function does not return anything, an implicit
“return None” statement gets added to the end of the function.


The inner myFunc(4) is printing out 0, 1, 2, 3 and it also returns None
which gets printed by the outer print() function.

>
> def myFunc(num):
> for i in range(num):
> return i
>
> print(myFunc(4))
> 0 #why just 0?


The return statement causes the loop to stop and the function to exit and
returns 0 to its caller which is the outer print() function.

Hope that helps.

Sri


>
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> https://mail.python.org/mailman/listinfo/tutor
>
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Function question

2017-03-25 Thread Alan Gauld via Tutor
On 25/03/17 10:01, Peter O'Doherty wrote:

> def myFunc(num):
>  for i in range(num):
>  print(i)
> 
> print(myFunc(4))
> 0
> 1
> 2
> 3
> None #why None here?

Because your function does not have an explicit return
value so Python returns its default value - None.
So the print() inside the function body prints the 0-3
values then the function terminates and returns the (default)
None to your top level print.

> def myFunc(num):
>  for i in range(num):
>  return i
> 
> print(myFunc(4))
> 0 #why just 0?

Because return always returns from the function immediately.
So you call the function, it enters the loop, sees the return for the
first element and exits. The print() then prints that returned value.

The preferred method to do what I think you were expecting is to build a
list:

def anotherFunction(num):
result = []
for i in range(num):
   result.append(i)
return result

Which is more concisely written using a list comprehension but
that would hide the general point - that you should accumulate results
in a collection if you want to return more than a single value.

To print the result you would typically use the string.join()
method:

print(' '.join(anotherFunction(4))


-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos


___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


[Tutor] Function question

2017-03-25 Thread Peter O'Doherty

Hi,

Apologies for the very basic question but could anyone explain the 
behaviour of these two functions (in Python3.5)?


def myFunc(num):
for i in range(num):
print(i)

print(myFunc(4))
0
1
2
3
None #why None here?


def myFunc(num):
for i in range(num):
return i

print(myFunc(4))
0 #why just 0?

Many thanks,
Peter


___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor