我要做的任务是显示所有其他元素,而不是(Americano)。我尝试使用'Record.remove(“ Americano”),但显示错误
Record.remove("Americano")
ValueError: list.remove(x): x not in list
我该怎么办......
========================这是==================的结果====== 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 ==========
答案 0 :(得分:2)
ValueError: list.remove(x): x not in list
您有错误,因为列表记录在记录中没有“ Americano”。
在将记录添加到“记录”之前,应检查记录是否不是“ Americano”
尝试一下:
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()
答案 1 :(得分:0)
您应该使用:
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()
答案 2 :(得分:0)
在删除元素之前,您应检查列表中是否有“ Americano”:
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 ==========