在 Python 上使用 Pickle 的应用程序在 运行 时崩溃

App Using Pickle On Python Crashing When Running

我曾尝试制作一个简单的记事本应用程序(未完成,所以这就是为什么“y”不完整)

import pickle
var1 = input("Open Last Note?(y/n)")
if var1 = "y":


if var1 = "n":
    note = input("Note")
    class MyClass()
        def __init__(self, param):
            self.param = param
 
    def save_object(obj):
        try:
            with open("data.pickle", "wb") as f:
                pickle.dump(obj, f, protocol=pickle.HIGHEST_PROTOCOL)
        except Exception as ex:
            print("Error during pickling object (Possibly unsupported):", ex)
 
    obj = MyClass(note)
    save_object(obj)

你的应用程序崩溃是因为这里有一个错误。下一个样本不正确:

if True:

if True:
    #comment

如果函数表达式没有主体,你必须写pass

if True:
   pass

if True:
   #comment
   pass

您也可以使用 Ellipsis:

if True:
    ...
#Elepsis

您的代码还有其他错误:

import pickle
var1 = input("Open Last Note?(y/n)")
if var1 == "y": #need double = in compare
    pass

if var1 == "n": #need == in compare
    note = input("Note")
    class MyClass(): #you miss colon
        def __init__(self, param):
            self.param = param
 
    def save_object(obj):
        try:
            with open("data.pickle", "wb") as f:
                pickle.dump(obj, f, protocol=pickle.HIGHEST_PROTOCOL)
        except Exception as ex:
            print("Error during pickling object (Possibly unsupported):", ex)
 
    obj = MyClass(note)
    save_object(obj)