无法加载或保存txt。文件

问题描述 投票:0回答:1

我写了一个代码,但显然它没有保存并加载到我的txt中。文件。如果您仔细阅读我的代码并告诉我有什么问题,我将不胜感激,因为我自己很难解决这个问题。我没有使用pickle,因为它在创建与编码相关的困难,所以我试图找到另一种解决方法,所有方法都保存到txt中。文件为“无”。预先谢谢你。

def savedata(x):
    play_again = input("Are you willing to save existing progress? Y/N")
    if (play_again == "Y") or (play_again == "y"):
        print("Saving progress...")
        file = open('adam_malysz.txt', 'w')
        file.write(str(x))
        file.close()
        print("Your file has been called - adam_malysz.txt")
        print("Progress has been successfully saved.")
    else:
        print("Returning to main menu")
def arrayfancy():
    num1 = int(input("Select size of an array: "))
    value = []
    for i in range(num1):
        value.append(random.randint(1, 99))
    print("Printing data...")
    print(value)
    print("Sorting Array...")
    bubblesort(value)
    print(value)
    print("Average value is: ")
    print(statistics.mean(value))
    print("Minimum value is: ")
    print(min(value))
    print("Maximum value is: ")
    print(max(value))
    print("Your data has been successfully printed")

    if choice == 1:
       savedata(arrayfancy())

python pycharm save load
1个回答
0
投票

您的arrayfancy()没有return语句,因此到达功能块的末尾时将返回None。然后,您的代码成功将“ None”写入文件。

您可以在return value的末尾添加arrayfancy(),这应该可以解决您的问题。

def savedata(x):
    play_again = input("Are you willing to save existing progress? Y/N")
    if (play_again == "Y") or (play_again == "y"):
        print("Saving progress...")
        file = open('adam_malysz.txt', 'w')
        file.write(str(x))
        file.close()
        print("Your file has been called - adam_malysz.txt")
        print("Progress has been successfully saved.")
    else:
        print("Returning to main menu")

def arrayfancy():
    num1 = int(input("Select size of an array: "))
    value = []
    for i in range(num1):
        value.append(random.randint(1, 99))
    print("Printing data...")
    print(value)
    print("Sorting Array...")
    bubblesort(value)
    print(value)
    print("Average value is: ")
    print(statistics.mean(value))
    print("Minimum value is: ")
    print(min(value))
    print("Maximum value is: ")
    print(max(value))
    print("Your data has been successfully printed")
    return value

if choice == 1:
    savedata(arrayfancy())
© www.soinside.com 2019 - 2024. All rights reserved.