hi guys,
I need some quick help to understand a code here. so on Mosh video on programming on Python on using dictionaries. so on the code he used, which he called, an output string to store the result of the dictionary.get() method
video with exact timestamp: Python Tutorial - Python for Beginners [Full Course] - YouTube
here is the code to better understand
phone = input('>')
digits_mapping = {'1' : 'one',
'2' : 'two',
'3' : 'three'
}
output = ''
for ch in phone :
output += digits_mapping.get(ch,'!')
print(output)
so why did he define that (output = ‘’) in the first place why cant he just use a normal variable below the for loop why did he increment the digits_mapping.get(ch,’!’) to that output variable ?
when can we use such technique of increment results to an empty variable instead of just using a variable directly without defining with an empty string and then increment it to the results that we want because I tried to put a normal variable as shown below to store the value of digits_mapping.get(ch,’!’) but it only prints the last entered number and not all numbers.
phone = input('>')
digits_mapping = {'1' : 'one',
'2' : 'two',
'3' : 'three'}
for ch in phone :
output = digits_mapping.get(ch,'!')
print(output)
`