dict enumerate(sequence, start=1)
:
dict(enumerate(['Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday'], 1))
dict.get()
:
wdays = {1: 'Sunday', 2: 'Monday', 3: 'Tuesday', 4: 'Wednesday', 5: 'Thursday', 6: 'Friday', 7: 'Saturday'}
def word(num):
return wdays.get(num, 'Error')
:
>>> word(3)
'Tuesday'
>>> word(10)
'Error'
>>> word('garbage')
'Error'
Depending on what you want to do with the string, it may not be advisable to return 'Error'
instead of just throwing an error. Otherwise, you will need to check if the line looks like a day of the week or is equal 'Error'
every time you use this function.
source
share