簡體   English   中英

如何在 Python 的函數中清除位置 arguments?

[英]How do I clear positional arguments in my function(s) in Python?

add 時出現循環錯誤,但現在我可以將數據添加到我的 txt 文件中,使用“see”列出我的數據時會出現此錯誤。 下面是錯誤:

TypeError:list_champs() 采用 0 位置 arguments 但給出了 1。

我看不到我在不知不覺中添加了一個在當前代碼中不需要的參數。

# file = open("champs.txt", "w+")

FILENAME = "champs.txt"

def write_champs(champs):
    with open(FILENAME, "w") as file:
        for champ in champs:
            file.write(champ + "\n")

def read_champs():
    champs = []
    with open(FILENAME) as file:
        for line in file:
            line = line.replace("\n", "")
            champs.append(line)
    return champs

def list_champs():
    for i in range(len(champs)):
        champ = champs[i]
        print(str(i+1) + " - " + champs)
    print()

def add_champ(champs):
    champ = input("Champion: ")
    #year = input("Season:    ")
    #champ = []
    champs.append(champ)
    #champ.append(year)
    write_champs(champs)
    print(champ + " was added.\n")

def display_menu():
    print("Premier League Champions")
    print("++++++++++++++++++++++++")
    print("COMMANDS")
    print("see  - See the list of Champions")
    print("add  - Add a Champion to the list")
    print("exit - Exit program")
    print()

def main():
    display_menu()
    champs = read_champs()
    while True:
        command = input("Enter command: ")
        if command == "see":
            list_champs(champs)
        elif command == "add":
            add_champ(champs)
        elif command == "exit":
            print("Later!")
            break
        else:
            print("Input not valid. Try again.")

if __name__ == "__main__":
    main()

一如既往的幫助非常感謝!

您需要更改您的def list_champs以支持 arguments:

def list_champs(champs):
    for i in range(len(champs)):
        champ = champs[i]
        print(str(i+1) + " - " + champs)
    print()

function定義

def list_champs():

function 調用

        list_champs(champs)

你想讓 function 接受爭論嗎? 根據您的預期設計修復一個或另一個。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM