-1

I am fairly new to python and i am struggling with appending inputs to a list of list. It can do it once but i want it to loop based on the yes or no question. when the list is outputted it doesn't output all inputs:

CategoryList =[['Rent'], ['Daily groceries'], ['Clothing'] ]

Categories = int(input("Please enter one of the following: \n 1 for Rent \n 2 for daily groceries \n 3 for clothing \n 4 to add a category: "))
if Categories == 1 or Categories == 2 or Categories == 3:
    Expense1 = input("Enter the amount: ")
    Expense1 = int(Expense1)
Choice = input("Would you like to continue yess or no: ") 
while Choice != "n":

    if Categories == 1:
        CategoryList[0].append(Expense1)
    if Categories == 2:
        CategoryList[1].append(Expense1)
    if Categories == 3:
        CategoryList[2].append(Expense1)
    if Categories == 4:
        CategoryNew = input("Enter the name of the new category: ")
        Expense = input("Enter the amount: ")
        NewAddition = [CategoryNew, Expense]
        CategoryList.append(NewAddition)
    if Choice =="n":
        break


Categories = int(input("Please enter one of the following: \n 1 for Rent \n 2 for daily groceries \n 3 for clothing \n 4 to add a category: "))
if Categories == 1 or Categories == 2 or Categories == 3:
    Expense1 = input("Enter the amount: ")
    Expense1 = int(Expense1)
Choice1 = input("Would you like to continue yess or no: ")

print(CategoryList)
Prune
  • 76,765
  • 14
  • 60
  • 81

1 Answers1

0

Basically you read the yes or not only outside the while loop, try putting it at the very end of the while loop, like this:

CategoryList =[['Rent'], ['Daily groceries'], ['Clothing'] ]

Categories = int(input("Please enter one of the following: \n 1 for Rent \n 2 for daily groceries \n 3 for clothing \n 4 to add a category: "))
if Categories == 1 or Categories == 2 or Categories == 3:
    Expense1 = input("Enter the amount: ")
    Expense1 = int(Expense1)
Choice = input("Would you like to continue yess or no: ") 
while Choice != "n":

    if Categories == 1:
        CategoryList[0].append(Expense1)
    if Categories == 2:
        CategoryList[1].append(Expense1)
    if Categories == 3:
        CategoryList[2].append(Expense1)
    if Categories == 4:
        CategoryNew = input("Enter the name of the new category: ")
        Expense = input("Enter the amount: ")
        NewAddition = [CategoryNew, Expense]
        CategoryList.append(NewAddition)
    if Choice =="n":
        break

    Choice = input("Would you like to continue yess or no: ") 


Categories = int(input("Please enter one of the following: \n 1 for Rent \n 2 for daily groceries \n 3 for clothing \n 4 to add a category: "))
if Categories == 1 or Categories == 2 or Categories == 3:
    Expense1 = input("Enter the amount: ")
    Expense1 = int(Expense1)
Choice1 = input("Would you like to continue yess or no: ")

print(CategoryList)

Before looping to while Choice != "n", it'll read the choice again, if it's anything different than "n", it'll repeat the flow.

I could'nt help but notice you are making also small mistakes, you don't need that if Choice == "n": break.

Another cool trick to make you code more readiable, is at the very beginning, instead of

if Categories == 1 or Categories == 2 or Categories == 3:

you can do:

if Categories in [1, 2, 3]:
H_DANILO
  • 321
  • 1
  • 9