创建一个可编辑列表。代码未显示错误,但未执行正确的功能

时间:2018-11-16 21:32:11

标签: python-3.6

我的代码有问题。 它不会产生错误,但是不会按预期运行。我试图制作一个程序,用户可以在其中添加,编辑,删除,查看计算机游戏列表中的项目。 但是,只要我输入任何内容 程序,它不会结束程序,但是什么也不做 我该怎么办?

computerGames = []
response = ""

def askQuestion():
    for each in ('add','delete','edit','view','end'):
    if each == 'view':
        print('Type',each,'to',each,'the list')
    elif each == "end":
        print("Type",each,"to",each,'program')
    else:
        print('Type',each,'to',each,'an item in(to) the list')
response = input("Enter your choice\n").upper()

def add():
    newUserGame = input("Enter a game to add onto the end of the list:\n")
    computerGames.append(newUserGame)
    print(computerGames)
    askQuestion()

def delete():
    userDeleteGame = input("Enter a game to delete:\n")
    if userDeleteGame in computerGames:
        computerGames.remove(userDeleteGame)
    else:
        print('Try again')
        print(computerGames)
        userDeleteGame = input("Enter a game to delete:\n")
    askQuestion()

def view():
    print("This is the current list of games:")
    print(computerGames)
    askQuestion()

def edit():
    editGame = input("Enter the game you want to replace:\n")
    if editGame in compGames:
        gameIndex = compGames.index(editGame)
        editGameNew = input("Enter the new game to replace this one")
        compGames[gameIndex] = editGameNew
    else:
        print("This item is not in the list")
        print(compGames)
        editGame = input("Enter the game you want to replace:\n")
    askQuestion()

askQuestion()

while response != "END":
    if response == "ADD":
        add()
    elif response == "DELETE":
        delete()
    elif response == "VIEW":
        view()
    elif response == "EDIT":
        edit()

正在进行GCSE计算,所以请原谅我缺乏编码能力。 在此先感谢您的帮助!

1 个答案:

答案 0 :(得分:0)

这是您代码的有效版本:

computerGames = []
response = ""

def askQuestion():
    for each in ('add','delete','edit','view','end'):
        if each == 'view':
            print('Type',each,'to',each,'the list')
        elif each == "end":
            print("Type",each,"to",each,'program')
        else:
            print('Type',each,'to',each,'an item in(to) the list')

    response = input("Enter your choice\n").upper()
    return response

def add():
    newUserGame = input("Enter a game to add onto the end of the list:\n")
    computerGames.append(newUserGame)
    print(computerGames)

def delete():
    userDeleteGame = input("Enter a game to delete:\n")
    if userDeleteGame in computerGames:
        computerGames.remove(userDeleteGame)
    else:
        print('Try again')
        print(computerGames)
        userDeleteGame = input("Enter a game to delete:\n")

def view():
    print("This is the current list of games:")
    print(computerGames)

def edit():
    editGame = input("Enter the game you want to replace:\n")
    if editGame in computerGames:
        gameIndex = computerGames.index(editGame)
        editGameNew = input("Enter the new game to replace this one")
        computerGames[gameIndex] = editGameNew
    else:
        print("This item is not in the list")
        print(computerGames)
        editGame = input("Enter the game you want to replace:\n")



while response != "END":
    response = askQuestion()

    if response == "ADD":
        add()
    elif response == "DELETE":
        delete()
    elif response == "VIEW":
        view()
    elif response == "EDIT":
        edit()

第一个问题是由于所谓的范围。在程序的顶部,您尝试实例化变量“ response”,然后在askQuestion函数中更新其值。但是,响应的空字符串版本是全局变量,因此可以在程序中的任何位置访问,但是响应的更新的用户输入版本是局部变量,因此无法在该函数外部访问,因为您尚未返回新变量因此,当您比较for循环中的“响应”值和每个if语句时,您获得的响应版本为空字符串。由于您没有else语句来捕获不等于add,delete,view,edit或end的输入,因此没有if / elif /(else)语句被触发,因此while循环重复进行。因此,我从askQuestion函数返回了局部变量“ response”,并在while循环中将全局变量“ response”的值设置为函数askQuestion的结果。

我还在while循环中而不是在每个“操作性”(想要一个更好的短语)函数的结尾处调用askQuestion函数,因为总是最好不要尽可能重复自己。

最后,我在编辑功能中将compGames的每个实例更改为computerGames,因为这是一个简单的语法错误。

我已经尽力向您解释了这一点,但要想一想起来,范围可能会非常复杂,因此请问我是否有任何问题。我希望这有帮助。