Re: [Tutor] function question
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
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
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