'Unbound variable error when variable is already defined (Python)

I have a strange issue with this code. What this code does is create a GUI where I input a numeric value, then select what I want to convert it using the Spin element and finally I display the result by updating the output text with the outputString variable. When I convert from Kg to Pound it works fine, displaying the message it's supposed to display, when I convert from Seconds to Min, to displays the following error: "NameError: name 'outputString' is not defined", and when I convert from Km to Miles it gives me the same error of: "NameError: name 'outputString' is not defined". Any ideas what may be happening?

import PySimpleGUI as sg

layout = [
    [
        sg.Input(key="-INPUT-"), 
        sg.Spin("Km to Miles", "Kg to Pound", "Seconds to Min", key="-UNITS-"), 
        sg.Button("Convert", key="-CONVERT-")
    ],
    [sg.Text("Output", key="-OUTPUT-")]
]

window = sg.Window("Converter", layout)

while True:
    event, values = window.read()

    if event == sg.WIN_CLOSED:
        break

    if event == "-CONVERT-":
            inputValue = values["-INPUT-"]
            if inputValue.isnumeric():
                match values["-UNITS-"]:
                    case "Km to Miles":
                        output = round(float(inputValue) * 0.6254)
                        outputString = f"{inputValue} km  are {output} miles."
                    case "Kg to Pound":
                        output = round(float(inputValue) * 2.205)
                        outputString = f"{inputValue} kg  are {output} pounds."
                    case "Seconds to Min":
                        output = round(float(inputValue) / 60)
                        outputString = f"{inputValue} seconds  are {output} minutes."
                window["-OUTPUT-"].update(outputString)

window.close()


Solution 1:[1]

outputString is currently only scoped inside the map statement, so you need to declare it outside of the match statement.

if event == "-CONVERT-":
        inputValue = values["-INPUT-"]
        if inputValue.isnumeric():
            outputString = ""
            match values["-UNITS-"]:
                case "Km to Miles":
                    output = round(float(inputValue) * 0.6254)
                    outputString = f"{inputValue} km  are {output} miles."

            window["-OUTPUT-"].update(outputString)

Now the problem is if no case gets matched outputString will be empty. Think about what needs to happen if no case is matched (perhaps add a default case case _: that throws an exception or writes a default value).

Solution 2:[2]

Apparently, the error was not due to the variable, but rather an error coming from the sg.Spin element of PySimpleGUI, as whenever it is clicked, the name changes, thus there was no valid case.

Solution 3:[3]

enter image description here

Missing brackets in your Spin Element. The parameter is a list of choices.

import PySimpleGUI as sg

layout = [
    [
        sg.Input(key="-INPUT-"),
        sg.Spin(["Km to Miles", "Kg to Pound", "Seconds to Min"], readonly=True, key="-UNITS-"),
        sg.Button("Convert", key="-CONVERT-")
    ],
    [sg.Text("Output", key="-OUTPUT-")]
]

window = sg.Window("Converter", layout)

while True:
    event, values = window.read()

    if event == sg.WIN_CLOSED:
        break

    if event == "-CONVERT-":
            inputValue = values["-INPUT-"]
            if inputValue.isnumeric():
                match values["-UNITS-"]:
                    case "Km to Miles":
                        output = round(float(inputValue) * 0.6254)
                        outputString = f"{inputValue} km  are {output} miles."
                    case "Kg to Pound":
                        output = round(float(inputValue) * 2.205)
                        outputString = f"{inputValue} kg  are {output} pounds."
                    case "Seconds to Min":
                        output = round(float(inputValue) / 60)
                        outputString = f"{inputValue} seconds  are {output} minutes."
                window["-OUTPUT-"].update(outputString)

window.close()

If you're using an IDE, like PyCharm, then the docstrings will easily flag these kinds of mismatch of types errors: enter image description here

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Eric Breyer
Solution 2 Jecs
Solution 3