1

I am trying to make a to do list app and I want to know how to make the code go back to the first line after I enter something into a list to add another item. Im new to Python BTW. As soon as they put enter input I want the program to execute the code then prompt them to add another item to the list.

print("Enter list item");
list_item = input();
list=list();
try:

    list_item

except:
    print('You didnt enter a task');
finally:
    list.append(list_item);
print(list);

Is there anyway to go back to line 1?

Babydoll
  • 29
  • 3
  • Possible duplicate of [Asking the user for input until they give a valid response](https://stackoverflow.com/questions/23294658/asking-the-user-for-input-until-they-give-a-valid-response) – l'L'l Apr 15 '19 at 00:46
  • No semicolons in Python. – Klaus D. Apr 15 '19 at 00:47
  • Also `finally` is not a guaranteed thing: https://stackoverflow.com/questions/49262379/does-finally-always-execute-in-python – knh190 Apr 15 '19 at 01:14

2 Answers2

2

I think this can be easily achived with a while true loop, which will keep the application in the loop as long as you need it.

The code for while true goes something like this:

while True:

----code----

And also make sure to not use semicolons ( ";" ) while working in python.

Your final code should look like this:

while True:
    print("Enter list item")
    list_item = input()
    list=list()
    try:
        list_item
    except:
        print("You didn't enter a task")
    finally:
        list.append(list_item)
    print(list)

Code indentation is an extremely important feature in Python. So you'll have to intend further the code you want to use inside that while loop.

Hope this helps!

Erik Cederstrand
  • 9,643
  • 8
  • 39
  • 63
  • When I do that the code executes nonstop meaning I dont even get to type in a list item because it keeps printing 'enter a task' over and over again. – Babydoll Apr 15 '19 at 02:04
0

When using the above method you can always break with ctrl-d or ctrl-c depending on your system.

Here is an example of while True... In this case it will break if you just press enter (no list item):

your_list = []
while True:
    item = input("Enter a list item\n")
    if not item:
        break
    your_list.append(item)
print(your_list)


james@VIII:~/Desktop$ ./test.py 
Enter a list item
one
Enter a list item
two
Enter a list item
three
Enter a list item
four
Enter a list item

['one', 'two', 'three', 'four']