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

时间:2018-12-12 21:32:58

标签: python python-3.x 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()

有没有办法使它工作?

1 个答案:

答案 0 :(得分:0)

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

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

然后可以通过

处理当前列表
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代码中根本不使用全局变量。

相关问题