How should i write my code


Hi, i’m a beginner with python ,
there is a code that i can’t figure how to write correctly:
i have to define a function lest’s say extra(hour) that will return “good morning” if hour is between 6am and 4PM, and “good afternonn” if hour is between 5 and 23 , for anything else , it shoud return “good night”: here what i did but it doesn’t work:

def extra(hour):
    for hour in range (0,24):
        if hour>=6 and hour<17:
            print("good morning")
        elif hour>=17 and hour<24:
            print("good afternoon")
            print("good night")

Thanks for your help


Why do you have a loop?


Ah , i just realised that it’s gonna repeat the seme thing , but then how should i precise that my hour parameter is only between 0 and 23?


simple conditions:

if input_valid:
   if morning:
   elif afternoon:
  inform user of invalid input

something like this.

The above is known as pseudocode, its just english of how your code should look structure wise


Great ! It worked, thank you :grinning:


i realized that if hour in range(0, 24) is also possible. But that is something very different then for


I see , i just tried that and it worked too , thaks a lot
I’ve got another question about another case , i find it uneasy:
It’s about defining a function that takes the values of a list and return this:
(number of elements )/(sum of inverses of elements ):
I know that number of elements is len(L),i thought i should define another function to return the inverses of elements but then i’ve got no idea how to get the sum of these inverses


so your inverse elements is a matrix? Please include code, this is too abstract without seeing how you implemented things


def harmonic(L):
def inverse (L):
for e in L:

It’s a mess
The question was to define a function such that if we have L(a,b,c) it returns : 3/ ( 1/a + 1/b + 1/c )


printing will make something show up on screen, it is useless as a means of passing around information inside the program

also, take greater care that you share exactly the same code as you’re working on - it should be able to copy it and run it and get the same outcome. try doing this from your posts, it’s quite a bit of extra work and may not be done the same way as what you have


Sorry can’t seem to copy correctly , for the print , i changed it to return


How many times does that function return and what does return do?


isn’t it supposed to command the program to run the devision (or the things we want as an output) ?


it refers to what? return?

if you don’t understand what return does (quite vital), it would be wise to gain more knowledge about this concept, return is a concept used in many programming languages. So you could learn more python.

It feels like you attempt to throw yourself into the deep, and sometimes that is fine, but it makes helping really difficult.


I’m not throwing myself since i have to do it , and i find it confusing wich is why i came here , what i understood when i started with python is that return run the function without showing it on screen , until we put print then we get the result …yest i still finding unclear as you saw so if you’ve got a better explanation i’m all ears or eyes since i’m reading


then by a long shot you do not understand what return does. Function calls is what will cause a function to run.

keywords are generally pretty descriptive, this also applies to return, it literally means return (handing back), now preferable without running the code, tell me the output:

def example():
   return 5

x = example()

now run it, to see if you where correct.

Given you usually hand something back after your done with it, the same applies to return. Once a return keyword is reached, the function is done, and stops.