[Solved]Short The Program | SoloLearn: Learn to code for FREE!

+9

[Solved]Short The Program

https://code.sololearn.com/cidhpbWB9g41/?ref=app I want to know is there any other way to do this code? I don't want these if- elif statements here Please give me some suggestions 🙏🙏🙏

31 Answers

New Answer

+10

Use a dictionary for the special cases: dict = { 10: "I am ten, you got it YAY!!!", 60: "I am not a senior okay!?", 100: "I am not a that old!", 0: "I was not born yesterday!" }; if age in dict: print(dict[age]); elif age > 50: print("I am younger then that!"); elif age < 10: print("I am older than that!"); else: print("not defined");

+6

Ternary operator https://www.sololearn.com/learn/Python/2500/ But short is different from good https://www.sololearn.com/post/223948/?ref=app https://code.sololearn.com/WKKkpq0efxai/?ref=app readability matters

+5

line 13 and 15 are never reached. == comparison should be placed before range comparison.

+5

Gordon 17 too, can you tell you any way to short this code

+5

Whether you like it or not I think if-else implementation is still the best in this case.. In all other examples you introduce additional data structures and complexity.

+5

We can use in other programming languages but this is python bro

+4

Do you have the answer ? KIIBO GHAYAL

+3

Gordon How will I put elif statements in Ternary Operator?

+3

Mainthing is how Sandra Meyer told. I have only one thing to add: You could import only the required function. from random import randint as ri You would not import the whole random module for one function. In addition one line 4 becomes shorter. And Gordon is right in 2 points. 1. Because how the statements are made, you will not reach line 13. 2. More shorter means not in any case more better. If you need a big dictionary, try to write a script for the script. Something like: num = 0 for items in range(101): r = "{a} : 'You are {b} years old',".format(a = num, b = num) print(r) s += 1 Then copypaste the result in your dict & change, if there are spezial statements on some ages.

+3

https://code.sololearn.com/ckdnHmcoYztp/#py

+3

We can implement switch in Python by using a dictionary called "switcher"

+2

~ swim ~ I agree with you that it's a best implementation but as we all know one problem can be solved in many ways So I was expecting a short way to make this code which will take less time

+2

I'd do it this way. The benefit of it is you can also easily print all the statements that are true. conditions=(a==0,a<10,a==10,a==20,a==60,a==100,a>40,a>50) statements=("I was not was not born yesterday!", "I am older than that!", "I am ten, you got it YAY!!!", "Younger than that!", "I am not a senior okay!?", "I am not a that old!", "YOUNGER!!!", "I am younger than that!") for condition,statement in zip(conditions,statements): if condition: print(statement) break

+2

Sandeep G R You can't use switch in this case, if you think you can i would love to see it.

+2

+2

𝐊𝐢𝐢𝐛𝐨 𝐆𝐡𝐚𝐲𝐚𝐥 , I have a solution for your problem. I'll code it down and share it here.

+2

𝐊𝐢𝐢𝐛𝐨 𝐆𝐡𝐚𝐲𝐚𝐥 , it is here. Although it won't be that revolutionary, but it might work (try including it in your code and following the instructions given): https://code.sololearn.com/cPA927XMZH9t/?ref=app

+1

Sandra Meyer good Idea , thanks Any other way 🤔

+1

U can use switch-case

+1

U can also go with ternary operators as well.