简体   繁体   English

Python 抛出 ValueError: list.remove(x): x not in list (not iterating over list)

[英]Python throws ValueError: list.remove(x): x not in list (not iterating over list)

I'm trying to solve the codewars kata of Vasya-Clerk.我正在尝试解决 Vasya-Clerk 的 codewars kata。 I'm getting the value error because I am removing items from the list.我收到值错误,因为我正在从列表中删除项目。 I read that I should make a copy of the list instead but since I am not iterating over the list itself but just appending and removing values it's not very clear to me how and where to do this copy with the command [:].我读到我应该制作列表的副本,但由于我没有遍历列表本身,而只是附加和删除值,所以我不太清楚如何以及在何处使用命令 [:] 执行此副本。 I know I could solve this maybe better with a dictionary but would like to finish it with lists if possible.我知道我可以用字典更好地解决这个问题,但如果可能的话,我想用列表来完成它。

Here are the instructions: The new "Avengers" movie has just been released.以下是说明:新的“复仇者联盟”电影刚刚上映。 There are a lot of people at the cinema box office standing in a huge line, Each of them has a single 100. 50 or 25 dollar bill.电影票房里有很多人排着长队,每个人手里拿着一张 100. 50 或 25 美元的钞票。 An "Avengers" ticket costs 25 dollars. “复仇者联盟”门票 25 美元。 Vasya is currently working as a clerk. Vasya 目前是一名文员。 He wants to sell a ticket to every single person in this line?他要卖给这一行的每一个人一张票? Can Vasya sell a ticket to every person and give change if he initially has no money and sells the tickets strictly in the order people queue, Return YES.如果 Vasya 最初没有钱并且严格按照排队的顺序出售门票,他能否向每个人出售一张票并找零,返回 YES。 if Vasya can sell a ticket to every person and give change with the bills he has at hand at that moment.如果 Vasya 可以把票卖给每个人,并用他当时手头的钞票找零。 Otherwise return NO否则返回NO

def tickets(people):  
    cash = []
    for bill in people:
      if bill == 25:
        cash.append(bill)
      elif bill == 50:
        if 25 in cash:
          cash.append(bill)
          cash.remove(25)
        else:
          return "NO"
      elif bill == 100:
        if 25 and 50 in cash:
          cash.append(bill)
          cash.remove(25)
          cash.remove(50)
        elif 25 and 25 and 25 in cash:
          cash.append(bill)
          cash.remove(25)
          cash.remove(25)
          cash.remove(25)
        else:
          return "NO"
      else:
        return "NO"

    return "YES"

test.assert_equals(tickets([25, 25, 50]), "YES")
test.assert_equals(tickets([25, 100]), "NO")

Your problem is in the following two lines:您的问题出在以下两行:

if 25 and 50 in cash:

and

elif 25 and 25 and 25 in cash:

Contrary to what it seems you assume, the first is just the same as 50 in cash and the second evaluates as 25 in cash .与您的假设相反,第一个与50 in cash相同,第二个评估为25 in cash Instead, you want to specify the conditions properly, and count the amount of 25 in cash.相反,您想正确指定条件,并计算 25 现金的数量。 They become:他们成为:

if 25 in cash and 50 in cash:

and

elif cash.count(25) >= 3:

Following @tmrlvi Just to finish your code...跟随@tmrlvi 只是为了完成你的代码......

def tickets(people):
    cash = []
    for bill in people:
        if bill == 25:
            cash.append(bill)
        elif bill == 50:
            if 25 in cash:
                cash.append(bill)
                cash.remove(25)
            else:
                return "NO"
        elif bill == 100:
            if 25 in cash and 50 in cash:
                cash.append(bill)
                cash.remove(25)
                cash.remove(50)
            elif cash.count(25) >= 3:
                cash.append(bill)
                cash.remove(25)
                cash.remove(25)
                cash.remove(25)
            else:
                return "NO"
        else:
            return "NO"

    return "YES"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM