String Formatting

  • + 1 comment

    It's because they're two different variables. width in line 2 belongs to the user program, while the other width belongs to the format function (noted by {width}).

    Although not exactly the same, a similar example would be a variable defined in a function versus after (and outside of) one:

    def func(x):
        return x + x
    
    x = 12
    func()
    

    Which returns the following:

    Traceback (most recent call last):
      File "<stdin>", line 5, in <module>
    TypeError: func() missing 1 required positional argument: 'x'