pythonpython-3.xsyntax-errorf-string

Error in syntax of fstrings in function input()


I have the following code in Python 3

def inputWithDefault(text:str, default):
    result = input(f"{" " * int(len(text))}{default}\r{text}")
    return default if not(result is type(default)) else result

Python says this when in the syntax-tree generation stage

  File "main.py", line 2
    result = input(f"{" " * int(len(text))}{default}\r{text}")
                                                             ^
SyntaxError: f-string: expecting '}'

This is difficult to work through as the error is unclear and its reasoning is difficult to parse, I have checked and the {} match up throughout the fstring declaration.


Solution

  • I have moved the spaces at the start into a variable spacing like so removing the errors

    def inputWithDefault(text:str, default):
        spacing = " " * int(len(text))
        result = input(f"{spacing}{default}\r{text}")
        return default if not(result is type(default)) else result