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");
+ 6
Ternary operator
https://www.sololearn.com/learn/JUMP_LINK__&&__Python__&&__JUMP_LINK/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
We can use in other programming languages but this is python bro
+ 4
Do you have the answer ? KIIBO GHAYAL
+ 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
We can implement switch in Python by using a dictionary called "switcher"
+ 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
𝐊𝐢𝐢𝐛𝐨 𝐆𝐡𝐚𝐲𝐚𝐥 , 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
U can use switch-case
+ 1
U can also go with ternary operators as well.
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).
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.