二手产品经理

二手产品经理

THIS IS RENO

Array - 35 days - Learn Python online for 100 days

Record#

  • Today I didn't learn any new knowledge points, just practiced using arrays.
  • I compared the correct answer with my code and I think the answer's code looks more like a programmer's code, more abstract. My code looks more like a diary. But that's what chatgpt said.
  • The first code is written by me, haha

These two pieces of code are simple programs for managing a to-do list. They both have the same basic functionality, but there are some differences in implementation and code structure. Below is a comparison of the advantages and disadvantages of these two pieces of code:

Advantages of the first code:

  • It uses more functions to encapsulate different operations, making the code more readable and maintainable.
  • It provides functionality for viewing, adding, editing, and deleting items in the to-do list, making it flexible and complete.
  • In the editing function, it first deletes the specified item and then adds a new one, maintaining consistency in operations.

Disadvantages of the first code:

  • It uses a global variable mylist to store the to-do list instead of encapsulating it in a class or function, which may limit the code's scalability and reusability.
  • The code contains some hard-coded parts, such as the command os.system("clear") to clear the terminal screen, which makes the code less flexible and portable.

Advantages of the second code:

  • It encapsulates the operations on the to-do list in different functions, making the code more readable and maintainable.
  • It uses the function printList() to print the to-do list, making the code structure clearer.
  • It adds a user confirmation feature when deleting items, improving the safety of operations.

Disadvantages of the second code:

  • It uses a global variable toDoList to store the to-do list, which may affect the code's scalability and reusability.
  • It lacks validation and error handling for user input, which may result in program crashes or incorrect results.
  • It lacks some functionality, such as the ability to edit specific items.

In summary, both of these pieces of code have some functionality and readability, but there is room for improvement in both. You can choose the code version that suits your specific needs and preferences, and make further improvements and extensions as needed.


import os, time

print("To Do List Manager")
print()

mylist = []


def view():
  for item in mylist:
    print(item)
  time.sleep(2)
  os.system("clear")


def add(l):
  if l in mylist:
    print(f"{l} in the list")
  else:
    mylist.append(l)
  print("add success")
  time.sleep(1)
  os.system("clear")


def remove(l):
  if l in mylist:
    mylist.remove(l)
  else:
    print(f"{l} not in list.")


while True:
  print(
    "Do you want to view, add, edit, or remove an item from the to do list?")
  menu = input()
  if menu == "add":
    mytxt = input("What do you want to add\n")
    add(mytxt)
  elif menu == "view":
    view()
  elif menu == "edit":
    mytxt1 = input("What do you want to remove\n")
    mytxt2 = input("What do you want to change it to?\n")
    remove(mytxt1)
    add(mytxt2)
  elif menu == "remove":
    mytxt1 = input("What do you want to remove\n")
    sure = input(f"remove: {mytxt1}. yes or no\n")
    if sure == "yes":
      remove(mytxt1)
    else:
      continue
Loading...
Ownership of this post data is guaranteed by blockchain and smart contracts to the creator alone.