用於提示使用者輸入號碼的功能

def input_number(msg, err_msg=None):
    while True:
        try:
            return float(raw_input(msg))
        except ValueError:
            if err_msg is not None:
                print(err_msg)

def input_number(msg, err_msg=None):
    while True:
        try:
            return float(input(msg))
        except ValueError:
            if err_msg is not None:
                print(err_msg)

並使用它:

user_number = input_number("input a number: ", "that's not a number!")

或者,如果你不想要錯誤訊息

user_number = input_number("input a number: ")