+ 1
which line would you use irl?
This is about the question about physical test score: #My solution: physic_test = int(input()) flight_test = int(input()) if physic_test >= 90 and flight_test >= 85: print(True) else: print(False) #We hadnt learned elsse statements yet, their solution was to add the results in a var and then print if true: physic_test = int(input()) flight_test = int(input()) result = physic_test > 90 and flight_test > 85 print (result) # I admit their solution is more clever then if else, but mine is way more readale right?
5 Antworten
+ 5
At this point there is no substantial difference between the two versions.
It is always a good idea to organize your code in self-contained, reusable chunks (functions, or sometimes even classes) which are responsible for a single task. Don't worry you will learn about this later.
Also, it is a good idea to separate code that just calculate something from a set of inputs, and code that interacts with the outside world (for example takes data from the user, or print text to the screen). From this point, the first example is a mixture of calculation and printing. The second code is better separated. This is the basic principle of functional programming paradigm.
This is how I would write it:
def test(physic, flight):
return physic >= 90 and flight >= 85
physic_test = int(input())
flight_test = int(input())
print(test(physic_test, flight_test))
+ 3
always it's important to write as possible clean and understandable to humans code, specially if you work with team of many developers.
Your solution (1st code) is more clean and easy for a developer to understand what it does
For python 1st code is always the best.
If it was a other language, like c++, i will go with the 2nd code because i can see the variable type, so instantly i know i waiting for a boolean result and it save me few lines, so i have smaller code.
+ 3
Great so now i've learned 3 ways to do this. This is why I like coding. There are so many ways to do the same thing!
+ 2
Yes, we create a function with the def keyword. The result of the function, represented by the return statement, can also be a different data type, it could be a number or a string or a list, etc.
+ 1
Tibor Santa Thx for the input, I've seen a lot of yt videos with that same idea, using def, instead of anything else. Just to clarify, def is to define a function right? And return is asking for bool of the and statement?
Anyway, your way seems to be the norm, and gets the best of both worlds, clever use of language and easy readability! Can't wait to have an actual lesson about it!