more conditions in a oneliner? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

more conditions in a oneliner?

is it possible to get a third condition in the oneliner? https://code.sololearn.com/cWRTS0NrRrrT/?ref=app

24th Aug 2023, 1:06 PM
Angela
Angela - avatar
3 Answers
+ 7
here are some general thoughts from me on the subject of one liners. > many of them are hard to read, difficult to understand and maintain, and it takes a great deal of effort to find errors when debugging them. > besides being very impressive to look at and challenging to create, one-liners are relatively useless in practice. if you read the coding guidelines from major companies or follow the zen of python you will understand why.
24th Aug 2023, 2:56 PM
Lothar
Lothar - avatar
+ 4
Angela, Yes you can add more conditions.. See this example.. `age = 16 is_student = False print("20% discount" if age < 18 and is_student else "Regular price" if age >= 18 else "Invalid age")` In this example:- #If the age is less than 18 and the person is a student, it will print "20% discount". #If the age is 18 or older, it will print "Regular price". #If the age is neither less than 18 nor 18 or older, it will print "Invalid age".
24th Aug 2023, 1:13 PM
Darpan kesharwani🇮🇳[Inactive📚]
Darpan kesharwani🇮🇳[Inactive📚] - avatar
+ 3
Here are a couple of ways to handle the logic and reduce the code. First way: make a decision between the two primary categories (above or below age 18), then check the smaller subdivisions below 18. print("Regular price" if age>=18 else "20% discount" if is_student else "10% discount") Second way: select a string to print between the two primary categories, and for the subdivided category use string interpolation with a calculation to modify the output string. print(("Regular price", f"{10*(1+is_student)}% discount")[age<18])
24th Aug 2023, 4:48 PM
Brian
Brian - avatar