Play this code and read output. I asked my question there. | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Play this code and read output. I asked my question there.

https://code.sololearn.com/cQPn1bDx4BnR/?ref=app

1st Jul 2019, 2:57 PM
MR Jey
MR Jey - avatar
4 Answers
+ 6
One-liner: print("".join(list(map(lambda x:dic[x.upper()], ow))))
1st Jul 2019, 3:34 PM
👑 Prometheus 🇸🇬
👑 Prometheus 🇸🇬 - avatar
+ 4
The print function has an end property, which is default to "\n". You can change it like this: print(ow, end = "")
1st Jul 2019, 3:06 PM
Airree
Airree - avatar
+ 4
For me there are some more doubts in this code. May be its worth to get what the code is doing: Here a revised code with some friendly comments: dic = {"N": "1", "A": "2", "M": "3", "E": "4",} ow = "Name" #**does not need brackets list1 = [] for i in ow.upper(): #i = i.upper() if i in dic: #ow = (dic.get(i)) #**(1) during current iteration in ow this variable is changed by this code line. This is should not be. #list1.append(ow) #**(2) ow, now containing numbers is appended to list1 list1.append(dic.get(i)) #**(3) This the correct way to pic values from the dict and append it to list1 print("".join(list1)) EDIT: https://code.sololearn.com/cS6xZRr7Qh87/?ref=app
1st Jul 2019, 6:10 PM
Lothar
Lothar - avatar
+ 3
#Changing keyword argument "end" is the easiest way, but here is another way: dic = {"N": "1", "A": "2", "M": "3", "E": "4",} ow = ("Name") list1 = [] for i in ow: i = i.upper() if i in dic: ow = (dic.get(i)) list1.append(ow) print("".join(list1)) print("\nThe output as u see is into 4 lines how can output be 1234 in one single line?")
1st Jul 2019, 3:11 PM
Seb TheS
Seb TheS - avatar