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")
else:
print("good night")
print(extra(hour)
print(extra(6))
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
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
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()
print(x)
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.