[Tutor] A Question about coding style
Roel Schroeven
roel at roelschroeven.net
Fri Sep 17 03:28:46 EDT 2021
Op 17/09/2021 om 3:35 schreef Manprit Singh:
> If I have to write a program, that allows the user to input a number and
> assign it to a variable b, if the number entered is a negative number,
> assign 0 to variable b.
> At last print the value of variable b. The code that i have written is
> given below:
>
> b = int(input("Enter a number"))
> if b < 0: b = 0
> print(b)
>
> See the whole if statement part is written in single line, is it acceptable
> ?
Another way for enforcing lower bounds (and similarly for upper bounds)
is using the max function:
b = max(0, b)
> Second example is of a for loop, which is also written in single line
> for i in range(6): print(i) # print values from 0 to 5,is it also
> acceptable ?
Personally I would advice against it, though I guess it can be
acceptable for very short simple things like that.
--
"Il semble que la perfection soit atteinte non quand il n'y a plus rien à
ajouter, mais quand il n'y a plus rien à retrancher."
"Perfectie is niet bereikt als er niets meer toe te voegen is, maar als er
niets meer weg te nemen is."
-- Antoine de Saint-Exupéry
More information about the Tutor
mailing list