Profilo di Jimmy Mcgill

Nome Jimmy Mcgill
Indirizzo email sebasteanwu@gmail.com
Website URLhttp://soundeffectgenerator.org
AvatarAvatar utenti
Messaggi1
  • Re: Numeri Romani
    Forum >> Principianti
    You have correctly identified that the code has several problems, both in syntax and structure. I will explain step by step what is wrong and how to improve it:



    ErrorUnboundLocalError :

    This happens because the variable k(and also c, d, uin the other methods) is used in returneven though it has not been initialized. If none of the conditions ifare met, the variable is never defined, causing the error.

    Solution: Initialize the variable at the beginning of the function, for example:


    def thousands(year):

    k = ""

    if year0 == "1": k = "M"

    elif year0 == "2": k = "MM"

    elif year0 == "3": k = "MMM"

    return k








    Incorrect access to year digits :

    You are trying to access yearas if it were a list of digits ( year 0, year 1etc.), but yearit is a string, so you have to use square brackets []to access individual characters. lipsync



    Incorrect or non-existent variable names attentionaid :

    In the function processing, you use DAinstead of D, which is the value returned by tens(). This will generate an error because DAit is not defined.



    Year as input :

    Remember to pass the year as a string to the functions, or convert it to a list of digits if you prefer to work this way:

    year = list(str(year).zfill(4)) # add leading zeros if year is e.g. "99"
    Non-standard representation of Roman numerals quit :


    For example, 4in Roman numerals it is written IV, not IIII. The same goes for 9which is IX, not VIIII. It just unit()has this anomaly.