Sololearn: Learn to Code
New course! Every coder should learn Generative AI!
Try a free lesson
+ 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");
30th Jun 2020, 4:39 AM
Sandra Meyer
Sandra Meyer - avatar
+ 5
line 13 and 15 are never reached. == comparison should be placed before range comparison.
30th Jun 2020, 4:21 AM
Gordon
Gordon - avatar
+ 5
We can use in other programming languages but this is python bro
1st Jul 2020, 12:57 PM
Sandy Sandeep
Sandy Sandeep - avatar
+ 4
Do you have the answer ? KIIBO GHAYAL
2nd Jul 2020, 3:14 AM
Sandy Sandeep
Sandy Sandeep - avatar
+ 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.
30th Jun 2020, 6:26 AM
Sven_m
Sven_m - avatar
30th Jun 2020, 8:27 PM
ODLNT
ODLNT - avatar
+ 3
We can implement switch in Python by using a dictionary called "switcher"
2nd Jul 2020, 2:47 AM
Sandy Sandeep
Sandy Sandeep - avatar
+ 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
30th Jun 2020, 1:34 PM
Chandan Kumar
Chandan Kumar - avatar
+ 2
𝐊𝐢𝐢𝐛𝐨 𝐆𝐡𝐚𝐲𝐚𝐥 , I have a solution for your problem. I'll code it down and share it here.
2nd Jul 2020, 6:35 AM
SSki11
SSki11 - avatar
+ 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
2nd Jul 2020, 6:56 AM
SSki11
SSki11 - avatar
+ 1
U can use switch-case
1st Jul 2020, 12:45 PM
Arya Deep Chowdhury
Arya Deep Chowdhury - avatar
+ 1
U can also go with ternary operators as well.
1st Jul 2020, 1:46 PM
Arya Deep Chowdhury
Arya Deep Chowdhury - avatar
0
If you would change the determined output in the way you have 1 text per decade, then you could use a simple array for all cases (divide age by ten and assign to index).
30th Jun 2020, 7:05 AM
Sandra Meyer
Sandra Meyer - avatar
0
Put your conditions in order, otherwise some conditions will never be checked. For example, a==60, a==100 and a==0 will never be checked in this code.
30th Jun 2020, 12:52 PM
Chandan Kumar
Chandan Kumar - avatar