Print output from a function in Python.

0 Cardinal Coog · September 22, 2015
I will paste the code below. My question is this: Why do I have to create a new variable in order to print the output of my function( ie, why can't I just print x and y instead of creating a new variable "womans_age". The function already returns y.)


def age_function(x):
    y = x/2+9
    return y

for x in range(18, 101):
    womans_age = age_function(x)
    print("Man's age: ", x, "Women's age: ", womans_age)

Post a Reply

Replies

Oldest  Newest  Rating
0 Halcyon Abraham Ramirez · September 23, 2015
def age_function(x):
   return x/2+9



for x in range(18, 101):
print("Man's age: ", x, "Women's age: ", age_function(x))


you actually can just like that
0 Cardinal Coog · September 23, 2015
Thank you both for your quick and informed replies.
0 Halcyon Abraham Ramirez · September 23, 2015
you're welcome and since your age_function is very short you can use a lambda function like this



for x in range(18, 101):
print("Man's age: ", x, "Women's age: ", (lambda x:x//2+9)(x))


It's basically your age function but unnamed
  • 1

Python

107,270 followers
About

This section is all about snakes! Just kidding.

Links
Moderators
Bucky Roberts Administrator