Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Function Help
#1
Hello Python Forums,

I am currently learning how to code in the wonderful language of Python. I am very beginner at computer programming (Python is my first language), so please let me know if the error I am making is super simple. I tired to make a very simple yet fun program just to demonstrate my ability to program a function and properly call it, as functions are my latest advent in Python. However, I seem to be a little lost. My code is down below.

maxFingers = 10
maxToes = 10
inputheight = input("What is your height in inches?")

def testaddition(): # here is part of my confusion
    fingers = input("How many fingers do you have?")
    toes = input("How many toes do you have?")
    total = fingers + toes
    return total

total = testaddition() # here is the second part of my confusion
print "You have", total, "miniature appendages."

numnails = lambda arg1, arg2: arg1 ** arg2 
print "You have", numnails(total, inputheight), "cells on your appendages."
This function is in no way mathematically correct (in the way that your height affect the number of nails you have), but it works as a tool for learning and practice. My issue is in the defining of the function. I can define it as it is up there; 
def testaddition()
And call it like it is above;
total = testaddition()
And the program will run just fine. What I don't seem to understand is why the function needs no arguments, or, when I put in an argument, like arg1, I can put in a random value. Here is a complete example of what I mean.
maxFingers = 10
maxToes = 10
inputheight = input("What is your height in inches?")

def testadditon(arg1): # to easily show what I mean
    fingers = input("How many fingers do you have?")
    toes = input("How many toes do you have?")
    total = fingers + toes
    return total

total = testadditon(0) # to easily show what I mean
print "You have", total, "miniature appendages."

numnails = lambda arg1, arg2: arg1 ** arg2 
print "You have", numnails(total, inputheight), "cells on your appendages."
The program will run the exact same. I would just like to understand why the number of arguments does not matter when I run the program. Thank you for any help you can give me.
Reply
#2
compiled languages will give you a warning saying there is an unused parameter in testaddition function. However python does not do this. It is just garbaged collected anyways. 

Quote:I would just like to understand why the number of arguments does not matter when I run the program. 
function by nature, can take any number of arguments. In fact you can unpack a list of unknown number of arguments
lister = [1,2,3]

def func(*args):
    print(args[0])
    print(args[1])
    print(args[2])
    
func(*lister)
Recommended Tutorials:
Reply
#3
(Nov-26-2016, 05:12 AM)Millionsunz Wrote: What I don't seem to understand is why the function needs no arguments
Because all input and calculation is inside the the function block.
(Nov-26-2016, 05:12 AM)Millionsunz Wrote: when I put in an argument, like arg1, I can put in a random value.
You can put in whatever you want because you do not use argument in function.
Now is argument been used in function,and return total + argument(arg1).
def testadditon(arg1): 
    fingers = input("How many fingers do you have?")
    toes = input("How many toes do you have?")
    total = fingers + toes
    return total + arg1 # Aliens has more fingers

alien_fingers = 100
total = testadditon(alien_fingers) # to easily show what I mean
print(total)
To spilt it up so you see that one function is only returting,
and the other function to calculation.
def body_parts():
    fingers = int(raw_input("How many fingers do you have?"))
    toes = int(raw_input("How many toes do you have?"))
    return fingers, toes

def body_calc(body_parts):
    fingers, toes =  body_parts()
    total = fingers + toes
    return total

if __name__ == '__main__':    
    print body_calc(body_parts)
As a new user you should be thinking of using Python 3.
Then you don't need to think of int(raw_input()),because input() is not safe(python 2.x).
Reply
#4
Also, you add one input to another as strings, so '10' + '2' for example will give you '102' as a result.
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#5
(Nov-27-2016, 05:12 AM)wavic Wrote: so '10' + '2' for example will give you '102' as a result.
Now do he/she use Python 2,so then is 12.
That's way i gave hint about int(raw_input()).
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020