+ 1

(Solved)Can I put dict on function ? Because when I put in, variable undefined,

Function

8th Jan 2021, 2:05 AM
Ratna13
Ratna13 - avatar
3 Antworten
+ 2
What are trying in code actually? Mention that. Why are you defining dictionary in function..? It becomes local to function.. Your code won't give any input. Because if condition type(i) == int is always false because your taking input as string type. And x is also a list of strings.. So I value in loop is always a string type. d.get('i') may through error because d defined in function, undefined outside. And d.get('i'), here key I is str type but you have to call with int type. So you may need d.get(int(i)). Hope it helps...
8th Jan 2021, 1:30 PM
Jayakrishna 🇮🇳
+ 1
Yes, Can you show your code so that we can find the problem and help you to fix it.
8th Jan 2021, 2:59 AM
Bibek Oli
Bibek Oli - avatar
+ 1
word =input() newword =[] def dict(): d = {1 : 'one', 2 : 'two', 3 : 'three', 4 : 'four', 5 : 'five', 6 : 'six', 7 : 'seven', 8 : 'eight', 9 : 'nine'} x = word.split() for i in x: if type(i) == int and i <10: print(d.get('i'))
8th Jan 2021, 4:09 AM
Ratna13
Ratna13 - avatar