Can this be done in less lines | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Can this be done in less lines

def test_func(x,y,z): total = int(x+y+z) if total == int(22): return "Correct: " + str(total) else: if total < int(22): incorrect = int(22) - total print("Answer off by the integer of " + str(incorrect)) elif total > int(22): incorrect2 = total - int(22) print("Answer off by the integer of " + str(incorrect2))

11th May 2019, 11:09 PM
Paras finn
Paras finn - avatar
10 Answers
+ 5
2 lines total=lambda x,y,z:'correct'+ str(x+y+z) if x+y+z==22 else "Answer off by the integer "+str(abs(22-(x+y+z))) print(total(10,10,7))
12th May 2019, 1:38 AM
abderrahim sallami
abderrahim sallami - avatar
+ 3
Diego 1line print(((lambda x,y,z:'correct'+ str(x+y+z) if x+y+z==22 else "Answer off by the integer "+str(abs(22-(x+y+z)))))(10,10,5))
12th May 2019, 1:50 AM
abderrahim sallami
abderrahim sallami - avatar
+ 2
def test_func(x,y,z): total = int(x+y+z) if total == int(22): print("Correct: " + str(total)) elif total < 22: print("Answer off by the integer of " + str(22 - total)) else: print("Answer off by the integer of " + str(total - 22))
11th May 2019, 11:27 PM
Anton Böhler
Anton Böhler - avatar
+ 2
Thank you Mr. Böhler for your time. i will study this.
11th May 2019, 11:29 PM
Paras finn
Paras finn - avatar
+ 2
total=lambda x,y,z:'correct'+ str(x+y+z) if x+y+z==22 else "Answer off by the integer "+str(abs(22-(x+y+z))) print(f'1:{total(10,10,7)}\n2:{total(12,8,2)}\n3:{total(5,15,5)}')
12th May 2019, 1:26 PM
abderrahim sallami
abderrahim sallami - avatar
+ 1
Three line version. You can even make it a oneliner. def test_func(x,y,z): total = int(x+y+z) return "Correct: "+str(total) if total==22 else "Answer off by the integer "+str(abs(22-total))
12th May 2019, 1:08 AM
Diego
Diego - avatar
+ 1
abderrahim sallami f=lambda t:t==22and"Correct: 22"or f"Answer off by integer {abs(22-t)}" [print(f(sum(i)))for i in([1,1,20],[42,4,2],[10,10,5])]
12th May 2019, 1:58 AM
Diego
Diego - avatar
0
Diego this gives no output for me
12th May 2019, 1:38 AM
Paras finn
Paras finn - avatar
0
Paras finn print(test_func(1,1,20)) print(test_func(42,4,2))
12th May 2019, 1:40 AM
Diego
Diego - avatar
0
abderrahim sallami Your code is a oneliner. Only the function itself is counted. PS: You can make it shorter.
12th May 2019, 1:44 AM
Diego
Diego - avatar