정재원
정재원

Reputation: 7

I am not able to remove elements from the list

The task that I have to do is showing all other elements rather than (Americano). I tried to use 'Record.remove("Americano") but it shows the error

Record.remove("Americano")
ValueError: list.remove(x): x not in list

What should I Do......

=========================this is the result of======================= Record.append(record[1]) print(Record) ->

['Menu_name', 'Cafe_latte', 'Americano', 'Americano', 'Smoothie_queen', 'Americano', 'Americano', 'Cafe_mocha', 'Cafe_latte', 'Americano', 'Amorparty', 'Plain_yogurt', 'Americano', 'Americano', 'Cafe_mocha', 'Berry_smoothie', 'Cafe_mocha', 'Cafe_latte', 'Americano', 'Cafe_latte', 'Berry_smoothie', 'Berry_smoothie', 'Plain_yogurt', 'Berry_smoothie', 'Plane_yougurt', 'Berry_smoothie', 'Cafe_latte', 'Americano', 'Americano', 'Cafe_mocha', 'Plain_yogurt', 'Americano', 'Plain_yogurt', 'Cafe_mocha', 'Plain_yogurt', 'Americano', 'Cofe_latte', 'Plain_yogurt', 'Cafe_mocha', 'Americano', 'Berry_smoothie', 'Cafe_latte', 'Cafe_latte', 'Cafe_mocha', 'Cafe_latte', 'Cafe_latte', 'Cafe_mocha', 'Americano', 'Plain_yogurt', 'Americano', 'Americano', 'Cofe_latte', 'Berry_smoothie', 'Berry_smudie', 'American_air', 'Plain_yogurt', 'Berry_smoothie', 'Berry_smoothie', 'Berry_smoothie', 'Cafe_latte', 'Americano', 'Plain_yogurt', 'Cafe_latte', 'Cafe_mocha', 'Cafe_mocha', 'Plain_yogurt', 'Berry_smoothie', 'Berry_smoothie', 'Cafe_mocha', 'Cafe_mocha', 'Berry_smoothie', 'Cafe_mocha', 'Plain_yogurt']

def load_menu():
    Menu = []
    #========= STEP 1 ==========
    menufile = open("menu.txt", "r")

    for line in menufile:
        menu = line.split()
        Menu.append(menu)

    menufile.close()

    for menu in Menu:
        print(menu[0]," : ",menu[1])
    #========= STEP 1 ==========
    return Menu

def error_check(date):
    #========= STEP 3 ==========
    Record = []
    recordfile = open("ledger_"+date+".txt","r")

    for line in recordfile:
        record = line.split()
        Record.append(record[1])
        Record.remove("Americano")
    print(Record)

    recordfile.close()
    #========= STEP 3 ==========

Upvotes: 0

Views: 38

Answers (3)

Zik
Zik

Reputation: 302

You should check if "Americano" in list before remove the element:

def error_check(date):
    #========= STEP 3 ==========
    Record = []
    recordfile = open("ledger_"+date+".txt","r")

for line in recordfile:
    record = line.split()
    Record.append(record[1])
    if "Americano" in Record:
        Record.remove("Americano")
print(Record)

recordfile.close()
#========= STEP 3 ==========

Upvotes: 0

Peter
Peter

Reputation: 130

ValueError: list.remove(x): x not in list

You have error because list Record don't have "Americano" in Record.

You should check record is not 'Americano' before add it to Record

Try this:

def error_check(date):
    #========= STEP 3 ==========
    Record = []
    recordfile = open("ledger_"+date+".txt","r")

    for line in recordfile:
        record = line.split()[1]
        if record != "Americano":
            Record.append(record)
    print(Record)
    recordfile.close()

Upvotes: 2

U13-Forward
U13-Forward

Reputation: 71570

You should use:

def error_check(date):
    #========= STEP 3 ==========
    Record = []
    recordfile = open("ledger_"+date+".txt","r")

    for line in recordfile:
        record = line.rstrip().split()
        Record.append(record[1])
        Record.remove("Americano")
    print(Record)

    recordfile.close()

Upvotes: 0

Related Questions