Python3: Error in function that has dictionary

Asked

Viewed 60 times

0

I have this code giving error in the assignment of the variable M, of the dictionary.

Returning the next: NameError: name 'm' is not defined

I’m a beginner in this kind of subject.

The object of it is returns a date in full.

data = input('Digite a data (DD/MM/AAAA): ')
def função_data(data, m):
    m = {
    "01":"janeiro",
    "02":"fevereiro",
    "03":"março",
    "04":"abril",
    "05":"maio",
    "06":"junho",
    "07":"julho",
    "08":"agosto",
    "09":"setembro",
    "10":"outubro",
    "11":"novembro",
    "12":"dezembro"}
    return data, m
print(função_data(f'{data[0:2]} de {m[0:3]} de {data[6:]}'))

It will be necessary to print the name before the return, I did it but unsuccessfully.

1 answer

2


Fixing up your code:

meses = {
    "01":"janeiro",
    "02":"fevereiro",
    "03":"março",
    "04":"abril",
    "05":"maio",
    "06":"junho",
    "07":"julho",
    "08":"agosto",
    "09":"setembro",
    "10":"outubro",
    "11":"novembro",
    "12":"dezembro"
}

data = input('Digite a data (DD/MM/AAAA): ')

def função_data(d, m):
    return d[0:2], m[d[3:5]], d[6:]
    
dia, mes, ano = função_data(data, meses)
    
print(f'{dia} de {mes} de {ano}')

If the goal is to return a full date use the module datetime.
To return the date formatted in a specific language and culture use the module locale

import datetime as dt
import locale 

try:
   locale.setlocale(locale.LC_TIME, locale.normalize('pt_BR.utf8'))
except locale.Error:
   print('Instale o módulo de línguagem adequado no seu Sistema Operacional.')

d= input('Digite a data (DD/MM/AAAA): ')
data = dt.datetime.strptime(d,"%d/%m/%Y")


print(data.strftime("%d de %B de %Y"))

Test on Windows: Teste no Windows Test on Linux: Teste no Linux

  • Augusto, thanks. A question, you whenever possible look for resolution in the documentation of something similar when it is programmed? I do that, but sometimes I can’t find anything close.

  • 1

    Yes, I always look the documentation.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.