#๐Ÿ”’ I don't understand why does it prints out "None"

196 messages ยท Page 1 of 1 (latest)

plain kindle
#

def is_leap_year(year):
if year % 4 == 0:
if year % 100 == 0 and year % 400 != 0:
return "This year is NOT a leap year."
elif year % 100 == 0 and year % 400 == 0:
return "This is a leap year! Now you get to suffer 1 more day than usual!"
else:
return "This year is Not a leap year"
print(is_leap_year(int(input("What year is this?"))))

I'm really new to python, so I'm sorry if i'm being a dumbass, I've yet to understand how the "return" function works...

icy plazaBOT
#

@plain kindle

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

grand jackal
plain kindle
#

I'm trying to take the user's input and to do some math with that input.

#

So it's not a specific year

grand jackal
#

any function that doesn't reach its return will have a value of None

plain kindle
#

Sorry if i'm confusing you! :(

grand jackal
#

so something is wrong with your conditions where none of the return paths are being followed

#

Let's look closely at your conditions

plain kindle
grand jackal
#
if year % 4 == 0:
#

this checks if the year is divisble by 4

#

and this looks good

#

but then you add two extra conditions after to check for

#
if year % 100 == 0 and year % 400 != 0:
    return "This year is NOT a leap year."
#

if the year is divisible by 100 but NOT by 400, return that it isn't a leap year

#

!e

def is_leap_year(year):
    if year % 4 == 0:
        if year % 100 == 0 and year % 400 != 0:
            return "This year is NOT a leap year."
        elif year % 100 == 0 and year % 400 == 0:
            return "This is a leap year! Now you get to suffer 1 more day than usual!"
    else:
        return "This year is Not a leap year"

year = 1700
print(is_leap_year(year))
icy plazaBOT
grand jackal
#

if we test with 1700, we can see that this works as intended

#
elif year % 100 == 0 and year % 400 == 0:
    return "This is a leap year! Now you get to suffer 1 more day than usual!"
#

then you check if it's divisible by 100 AND 400

#

there's really no need to check for both

#

if it's divisible by 400, then of course its also divisible by 100

plain kindle
#

ooooh

grand jackal
#

but

#

even this check is not necessary

#

by the time the code reaches this point, we've already established that the year is divisible by 4

#

and we've already established that the year is not divisible by 100 but not 400

#

which means, all years that have made it this far must be leap years

#

however, there's still a flaw with the overall design of this function

#

is_leap_year is a function that should return True for leap years and False for non-leap years

#

you aren't returning True or False, you're returning strings

plain kindle
#

oh, so I should use booleans more often here?

grand jackal
#

don't look at it as "use them more often"

#

generally functions that begin with is will return True or False

#

we're checking if something is True or False

plain kindle
grand jackal
#

it's just a naming convention

#

it doesn't force anything

#

look at string methods for example

#

!e

print(*[i for i in dir(str) if i.startswith('is')], sep=', ')
icy plazaBOT
grand jackal
#

don't worry about the code, just look at the result

#

these are all the string methods that begin with is

#

they all check if a string contains specific characters. These all return bool

plain kindle
#

Oh, so if a function begins with an "is" It'll return either "True" or "False"?

grand jackal
#

it's not mandatory, but it's a pretty reliable rule

plain kindle
grand jackal
#

it's not important

#

I was just trying to generate the results

plain kindle
#

o

grand jackal
#

so you said you're still not quite sure what return does?

plain kindle
#

Yup, I've just learned about it yesterday, but as far as I've seen, a lot of codes use this function.

grand jackal
#

in your own words, what would you say a function is used for?

plain kindle
#

return marks an end of a function, that's one

grand jackal
#

I'm not interested in the syntax. What is a function?

#

What does it do? What would you need one?

plain kindle
#

It... Acts as an.... output

#

..?

#

of a function

grand jackal
#

I'm not asking about what return is. I'm asking about what a function is.

plain kindle
#

oh

#

sry

#

function is a specific set of various... acts...?

#

idk how to properly say this)

grand jackal
#

That's ok, I'm just trying to work out what you know and how well you know it

#

A function is simply a pre-defined set of instructions that we can then run later

plain kindle
#

yup, there's also an input function, like "this_stuff(stuff2, stuff3)"

grand jackal
#

I wouldn't refer to that as an input function

#

defining your own functions are kind of a 2 step thing

#

step 1 is defining it

#

and step 2 is calling it

plain kindle
#

oh, oke

grand jackal
#
def foo():
    print("function foo is being called")
#

first we define it

#

if we ran this, it would appear that nothing happens

plain kindle
#

yup

grand jackal
#

!e

def foo():
    print("function foo is being called")
icy plazaBOT
plain kindle
#

foo()

grand jackal
#

exactly

plain kindle
#

then it would actually run

grand jackal
#

!e

def foo():
    print("function foo is being called")


foo()
foo()
foo()
icy plazaBOT
grand jackal
#

functions are also nice because we can reuse them as many times as we want

#

it really helps you cut down on writing repetitive code

plain kindle
#

yup, and not to print gigantic code :D

grand jackal
#

ok, so it's important to understand that printing has no value

#

it's a function used to display output

#

when you're a beginner, pretty much everything you do is going to be focused on trying to print some sort of result

plain kindle
grand jackal
#

but as you write more and more advanced code, the goal won't always be to print something

#

let's say I want to write a script that takes every image in a folder and shrink it by 50%

#

that doesn't really require any printing

plain kindle
#

ooh

#

I see

grand jackal
#

!e

x = print(123)
icy plazaBOT
grand jackal
#

if I were to do print(x) on the next line, what do you think would happen?

plain kindle
#

It would print 123 again, perhaps? Because you've already set a variable called "x", where you print a specific number

grand jackal
#

that's not quite how functions work

plain kindle
#

:(

grand jackal
#

when we do x = print(123), we aren't storing the function call in the variable

plain kindle
#

ooh

grand jackal
#

what happens is print(123) will be called first

#

and EVERY function has a return

plain kindle
grand jackal
#

whatever is returned basically replaces the call

#

any function that doesn't directly provide a return will automatically return None

#

print always returns None

#

let's look at a better example

#
def foo():
    return 5
#

this isn't a very exciting function, but what's important is that it returns 5

#
def foo():
    return 5


result = foo()
plain kindle
grand jackal
#

the way a function is called doesn't change whether or not it has a return

#

returning None is still a return

#

it's just not a useful return

plain kindle
grand jackal
#

foo() gets called

#

it returns 5

#

so foo() will essentially become the value 5 when it is called

#
result = foo()
#

as far as python is concerned here, all it sees is result = 5

#

the value gets returned to the caller

plain kindle
#

ooh, so, basically "foo =5"?

grand jackal
#

!print-return

icy plazaBOT
#
Print and return

Here's a handy animation demonstrating how print and return differ in behavior.

See also: /tag return

grand jackal
#

Watch this gif a few times

mighty loom
#

Hey @grand jackal ... when you finish can you please come to the My pygame game keeps free....

mighty loom
#

ok

plain kindle
# icy plaza

oooh, so, basically, my_function is set at 5, then the computer sees a that y = my_function, so it does manipulation inside this function

grand jackal
#

whenever we call a function, we simply run the code inside the function until it reaches a return

#

then whatever is returned becomes the value where it was called from

plain kindle
#

and then sets an r variable to this math operation

#

ooooooooo

grand jackal
#

are you familiar with the len() function?

plain kindle
#

so you can kinda say, that my_function = 7>

plain kindle
grand jackal
plain kindle
#

I've used it on strings mostly

plain kindle
grand jackal
#

!e

result = len('abcde')
print(result)
icy plazaBOT
grand jackal
#

the len function is called on the string 'abcde'. It goes and finds out how long the string is, and returns 5

#

so result = 5

plain kindle
#

so it sets a specific value to a, let's say, string, and then the user can use the data, that's outputted from it!?

grand jackal
#

exactly

#

returning is how we can use a function to do some sort of calculation, and then give us the result

plain kindle
#

so the user can basically "create their own len()"?

grand jackal
#

you can create whatever you can think of ๐Ÿ™‚

plain kindle
#

well, yeah)))

grand jackal
#

for example, a function that checks whether or not a year is a leap year

#
year = 2024
result = is_leap_year(year)
plain kindle
#

and then we can, through different math manipulations, print out "result" and it will RETURN in a boolean?

grand jackal
#

remember the goal isn't always to print

plain kindle
#

oh yeah

grand jackal
#

maybe we want to clean up all the files on our computer

#

and we want to delete all files created in a leap year

plain kindle
#

then the print function will be rendered obsolete in this particular case

grand jackal
#
for file in files:
    year = file.get_year()
    if is_leap_year(year) is True:
        file.delete()

#

this isn't quite real method names, but it's to give you an idea

plain kindle
#

I see

grand jackal
#

look at every file
get the year of the file
check if the year is a leap year
if it is, delete it

#

you can see here we don't always need to store the return in a variable

#

I'm just directly doing if is_leap_year(year)

plain kindle
#

oke, oke

grand jackal
#

The biggest problem in your original code is that not all paths led to a return

plain kindle
#

So the biggest problem with the original code is: 1 - sets too many unneseccary checks

#

that can be avoided

#

i see

#

K, with that knowledge in mind i'll try to fix it. But before I go, may I ask you a question that's sort of unrelated to this?

grand jackal
#
def check_name(name):
    if name == 'timmy':
        return True
    elif name == 'sally':
        return True


result = check_name('bob')
#

look at this simple example

plain kindle
#

ooh

grand jackal
#

this function only returns True if your name is timmy or sally

#

if your name is bob, nothing gets returned

plain kindle
#

i see

plain kindle
# grand jackal Sure

I'm just concentrated on learning things on my own and I kinda feel like I'm cheating by using methods like these, are my fears not justified?

grand jackal
#

Methods like what? Discord help?

plain kindle
#

yup

grand jackal
#

How would it be cheating?

#

I didn't directly hand you the answer

#

you learned something new

#

and now you can take what you learned and try to apply it to your own code

#

asking questions is a great way to learn

#

as long as you continue to practice what you learn, you'll be ok

plain kindle
#

Thank you! I'm really new to all of this, so it does feel a little overwhelming, but it's interesting as heck! Thx once again! :D

grand jackal
plain kindle
#

Yuipee! I did it :D And now I also understand the functions better!!! This was a hard coding challenge ngl)

icy plazaBOT
#
Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.