#๐Ÿ”’ [Code review] To-Do list project by beginner

35 messages ยท Page 1 of 1 (latest)

golden swallow
#

Hello. I just moved from c++ when i undrstud that c++ is to hard for me and i moved to python and i tried to make small to do list project and i would like to have feedback about this code hove much mistakes i made and dont be shy say all what you want here about code i will take your advices and will learn from them.

import os
import time

def options():
   print('\nTODO-LIST')
   print('1. Add task')
   print('2. View tasks')
   print('3. Remove tasks')
   print('4. Close program')
   print('5. Save tasks in txt file')

def list(array):
   for x, array in enumerate(array, start=0):
       print(f"{x}. {array}")  

array = []

while True:
   options()
   choice = input("Option: ")
   try:
       check = int(choice)       
       if check == 1:
           task = input ('How much tasks you want to add: ')
           for x in range(int(task)):
               newtask = input('New task: ')
               array.append(newtask)
               print('Task successfully added')
       elif check == 2:            
           print('2')        
       elif check == 3:               
           list(array)
           while True:
               delete = input('Number fo choice for delete: ')          
               if int(delete) <= 1 <= len(array) :
                   print('Works')
                   break
               else:
                   print(f'Number need to be betwen 0 - {len(array) - 1}')
       elif check == 4:
           print()
       elif check == 5: 
           print('Saved')
   except:        
       print('Option needs to be number') 

@whole sequoia

potent robinBOT
#

@golden swallow

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

whole sequoia
#

sup

#

lemme get my food

golden swallow
#

Okey ๐Ÿ˜„

whole sequoia
#

you also shouldnt name anything in your program after any of python's builtins

#

because you override the original definition

#

!e ```py
print(list("abc"))

```py
list = 1

print(list("abc"))
potent robinBOT
whole sequoia
#

see how the first one works and the second one doesnt?

golden swallow
#

yes but why? ๐Ÿ˜„

whole sequoia
#

list is a number, not a list converter

#

so when we try to do list(x) again, that's doing 1(x)

#

which makes no sense

#

so python raises an error

#

also don't have massive try-except blocks

golden swallow
#

are you okey if ask why? ๐Ÿ˜„

whole sequoia
#

yeah sure go ahead

whole sequoia
#

also you can always ask why

#

we're here to help

whole sequoia
golden swallow
#

It's better to have multiple try except than one large?

golden swallow
whole sequoia
#

keep them as short as possible

#

!except

potent robinBOT
#
Error handling

A key part of the Python philosophy is to ask for forgiveness, not permission. This means that it's okay to write code that may produce an error, as long as you specify how that error should be handled. Code written this way is readable and resilient.

try:
    number = int(user_input)
except ValueError:
    print("failed to convert user_input to a number. setting number to 0.")
    number = 0

You should always specify the exception type if it is possible to do so, and your try block should be as short as possible. Attempting to handle broad categories of unexpected exceptions can silently hide serious problems.

try:
    number = int(user_input)
    item = some_list[number]
except:
    print("An exception was raised, but we have no idea if it was a ValueError or an IndexError.")

For more information about exception handling, see the official Python docs, or watch Corey Schafer's video on exception handling.

whole sequoia
#

i gotta eat now, brb soon

golden swallow
#

Okey. have a good meal ๐Ÿ™‚

potent robinBOT
#
Python help channel closed for inactivity

This help channel has been closed. Feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.