python - 如何使用基于函数内用户输入的方法来更改类

标签 python python-3.x class

构建一个简单的待办事项列表应用程序来自学类(class)。几乎一切正常(到目前为止,您可以添加项目、查看列表和删除项目)。但是,我似乎不知道如何实现更改项目功能。这应该会更改已创建项目的名称,但我正在处理的代码没有执行此操作。

# counters for telling if a slot is empty or not
a = 0
b = 0
c = 0
d = 0
e = 0

# overarching class for entire program
# outlines how a list item is structured
class Item():

    def __init__(self, thingToDo, dueDate, priority):
        self.thingToDo = thingToDo
        self.dueDate = dueDate
        self.priority = priority

    def thingChange(self):
        thingChange = input("What would you like to change it to? ")
        self.thingToDo = thingChange


def whatToChange():
    global whatToChange
    whatToChange = input("What in the item would you like to change (1. thing, 2. due date, 3. priority)?")
    if "1" in whatToChange:
        itemChange = input("What would you like to change it to? ")
        item1.thingToDo = itemChange
        printCurrentList()


# takes inputs to add an instance of to do item
def getItem():
    global thing
    thing = input("What do you want to do?")
    global time
    time = input("When do you need to do it by?")
    global importance
    importance = input("Priority (rate out of 5): ")

# each of these takes the inputs from getItem(), and puts them into the slots
def runItem1():
    getItem()
    global item1
    item1 = Item(thing, time, importance)

    print("1. You need to " + item1.thingToDo + " by " + item1.dueDate + ", and the priority is " + item1.priority + "/5.")
    global a
    a = a + 1


def runItem2():
    getItem()
    global item2
    item2 = Item(thing, time, importance)

    print("2. You need to " + item2.thingToDo + " by " + item2.dueDate + ", and the priority is " + item2.priority + "/5.")
    global b
    b = b + 1


def runItem3():
    getItem()
    global item3
    item3 = Item(thing, time, importance)

    print("3. You need to " + item1.thingToDo + " by " + item1.dueDate + ", and the priority is " + item1.priority + "/5.")
    global c
    c = c + 1

def runItem4():
    getItem()
    global item4
    item4 = Item(thing, time, importance)

    print("4. You need to " + item4.thingToDo + " by " + item4.dueDate + ", and the priority is " + item4.priority + "/5.")
    global d
    d = d + 1

def runItem5():
    getItem()
    global item5
    item5 = Item(thing, time, importance)

    print("5. You need to " + item5.thingToDo + " by " + item5.dueDate + ", and the priority is " + item5.priority + "/5.")
    global e
    e = e + 1

# pretty self explanatory, prints out the current slots in a list-type format
def printCurrentList():
    if a > 0:
        print("1. You need to " + item1.thingToDo + " by " + item1.dueDate + ", and the priority is " + item1.priority + "/5.")
    else: print("Slot 1 is empty.")

    if b > 0:
        print("2. You need to " + item2.thingToDo + " by " + item2.dueDate + ", and the priority is " + item2.priority + "/5.")
    else:
        print("Slot 2 is empty.")

    if c > 0:
        print("3. You need to " + item1.thingToDo + " by " + item1.dueDate + ", and the priority is " + item1.priority + "/5.")
    else:
        print("Slot 3 is empty.")

    if d > 0:
        print("4. You need to " + item4.thingToDo + " by " + item4.dueDate + ", and the priority is " + item4.priority + "/5.")
    else:
        print("Slot 4 is empty.")

    if e > 0:
        print("5. You need to " + item5.thingToDo + " by " + item5.dueDate + ", and the priority is " + item5.priority + "/5.")
    else:
        print("Slot 5 is empty.")

# adds an item to the list, but first, checks to see if list slots are already used -- uses a,b,c,d,e
def add():
    printCurrentList()
    if a > 0 and b > 0 and c > 0 and d > 0 and e > 0:
        print("You need to empty a slot before you add another item.")
        whichToRemove = input("Which slot would you like to remove?")
        if input == "1":
            print("What would you like to add in its place?")
            runItem1()
    else:
        slot = input("Which slot would you like to use?: ")
        if slot == "1":
            runItem1()
        elif slot == "2":
            runItem2()
        elif slot == "3":
            runItem3()
        elif slot == "4":
            runItem4()
        elif slot == "5":
            runItem5()
        else:
            print("Error. Please try again.")
            add()

# main loop of the program
def main():
    prompt = input("What do you want to do (add, remove, change, or view)?: ")
    if prompt == "add":
        add()
    elif prompt == "remove":
        printCurrentList()
        whichToRemove = input("Which one would you like to remove?: ")
        if whichToRemove == "1":
            print("Item 1, " + item1.thingToDo + ", has been removed.")
            global a
            a = 0
        elif whichToRemove == "2":
            print("Item 2, " + item2.thingToDo + ", has been removed.")
            global b
            b = 0
        elif whichToRemove == "3":
            print("Item 3, " + item3.thingToDo + ", has been removed.")
            global c
            c = 0
        elif whichToRemove == "4":
            print("Item 4, " + item4.thingToDo + ", has been removed.")
            global d
            d = 0
        elif whichToRemove == "5":
            print("Item 5, " + item5.thingToDo + ", has been removed.")
            global e
            e = 0
        printCurrentList()

    elif prompt == "view":
        print("Here is your current list: ")
        printCurrentList()

    elif prompt == "change":
        whichToChange = input("Which one would you like to change?: ")
        if "1" in whichToChange:
            oldItem = item1.thingToDo
            whatToChange()
            thingChange()
            print("Item 1, " + oldItem + ", has been changed to " + item1.thingToDo)
        elif "2" in whichToChange:
            print("Item 2, " + item2.thingToDo + ", has been changed to _____________.")
        elif "3" in whichToChange:
            print("Item 3, " + item3.thingToDo + ", has been changed to _____________.")
        elif "4" in whichToChange:
            print("Item 4, " + item4.thingToDo + ", has been changed to _____________.")
        elif "5" in whichToChange:
            print("Item 5, " + item5.thingToDo + ", has been changed to _____________.")
        printCurrentList()
    main()

main()

有办法让它工作吗?

最佳答案

看起来您正在将过程方法与面向对象的方法混合在一起。 面向对象设计的原则之一是确定函数所属的位置……哪些函数属于哪个类。 一旦我自己解决了这个问题,从程序性思考转变为从对象角度思考对我来说就变得容易多了。

我建议创建一个列表 [] 或字典 {} 来存储您的项目。

然后打印当前列表可以通过

处理
item_list = []
# ... create items and add to item_list using item_list.append(item)

for item in item_list: 
     print(item)

item_dictionary = {}
# create items and add to item_dictionary using item_dictionary[some_key] = item
for key in item_dictionary:
    item = item_dictionary[key]

然后添加项目类所需的任何函数,并使用方法在字典列表中添加/删除/编辑这些项目。

我也会按照评论员的建议放弃全局变量。我在编写的 Python 代码中根本不使用全局变量。

关于python - 如何使用基于函数内用户输入的方法来更改类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53751684/

相关文章:

python - 将所有大写字符替换为 'X',将所有小写字符替换为 'x',同时保持所有空格或符号相同

python - 服务器如何启动与客户端的连接

python - 安装了 Python 3.8.5,但我的终端仍然检测到 Python 2.7(Mac 上的 Catalina)

python - ImportError [E048] 无法从 spacy.lang 导入语言 en

java - java中构造函数类型不匹配

c++ - 什么时候应该在 C++ 中使用类和结构?

Python、列表+字典、类?

python - 第一批后 Tensorflow ResourceExhaustedError

Python 序列化类并使用 JsonPickle 更改属性大小写

c++ - 为多个输入选择选项