# How should i write my code

#1

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))
``````

#2

Why do you have a loop?

#3

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?

#4

simple conditions:

``````if input_valid:
if morning:
elif afternoon:
else:
else:
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

#5

Great ! It worked, thank you

#6

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

#7

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

#8

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

#9

def harmonic(L):
def inverse (L):
for e in L:
print(1/e)
print(len(L)/(sum(inverse(L))
print(harmonic([1,2,3])

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 )

#10

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

#11

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

#12

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

#13

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

#14

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.

#15

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

#16

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.