-5

I want to creat a new list by operating some elements of a price list, but the new list is empty. What am I doing wrong?

prices = [2, 50, 70, 30, 555]
new_prices =[]

if int in prices > 20 and int in prices <= 50: 
       new_price.append(int * 0.4)
elif int in prices > 50: 
       new_price.append(int * 0.6)
elif int in prices == 5: 
        new_price.append(int * 0)

print(new_prices)
MountQ
  • 21
  • 3
  • 2
    What do you think `if int in prices > 20 and int in prices <= 50` does? This is a decent attempt at writing some code but illustrates a fundamental misunderstanding of python. I suggest you back up a little bit and learn how to take an action on each item in a list. We do this with a `for` loop. – Code-Apprentice Sep 20 '21 at 19:56
  • 1
    And the specific thing you need to learn for your current problem is called a "for loop". – Code-Apprentice Sep 21 '21 at 20:02

4 Answers4

2

I believe you're a little confused on the specific syntax of Python. One thing to note is that while using int works here, it should not be done. In Python int is a built-in type that your code is overwriting. You should also have a loop to actually iterate over every element of the list. Take this code for example:

prices = [2, 50, 70, 30, 555]
new_prices =[]
for price in prices:
    if price > 20 and price <= 50:
        new_prices.append(price * 0.4)
    elif price > 50:
        new_prices.append(price * 0.6)
    elif price == 5:
        new_prices.append(0)
BTables
  • 4,413
  • 2
  • 11
  • 30
1

Your problem seems to be a confusion between how to iterate through values in python and how if statements work.

If statements simply check whether a given condition is true or not and execute a specific piece of code in the case it is.

So in the first if statement, what you are doing is checking whether the variable int is inside the list prices is greater than 20... As you can see this sentence doesn't make sense grammatically, because you are trying to check two conditions at the same time without any relation (connectors) between them. On one side you want to know if int is in prices (which already raises the problem that int hasn't been defined) and on the other if 'something' is greater than 20.

My impression of what you wanted to do is something in the lines of: for every value in prices do something if the value is greater than 20. Here you actually would need to go through or iterate for every value within the list prices. This can be achieved by using a for loop, a while loop with a counter or list comprehensions. A good example of how to solve this problem with a for loop can be found in @BTables answer.

CRuizH
  • 70
  • 1
  • 10
0

There seems to be a typo: first you initiate “new_prices” but you append stuff to “new_price”

Furthermore, I would write the if condition like this

for price in prices:
    if price > 20 and price <= 50:
…
MattiH
  • 554
  • 5
  • 9
-2
prices = [2, 50, 70, 30, 555]
new_prices =[]
for int in prices:
    if int > 20 and int <= 50: 
           new_prices.append(int * 0.4)
    elif int > 50: 
           new_prices.append(int * 0.6)
    elif int == 5: 
            new_prices.append(int * 0)

print(new_prices)

You probably want something like the above. Note your original code had new_price and new_prices.

Output

[20.0, 42.0, 12.0, 333.0]
randomer64
  • 70
  • 6
  • 1
    While this works you're overwriting a built-in type. This will *technically* work, but it's very bad practice and should not be done. – BTables Sep 20 '21 at 20:04
  • 2
    @not_speshal int is not a reserved keyword. https://www.tutorialspoint.com/What-are-Reserved-Keywords-in-Python – randomer64 Sep 20 '21 at 20:07
  • @BTables int() is a function. int is a valid value. – randomer64 Sep 20 '21 at 20:10
  • [In general, using variable names that eclipse a keyword or built-in function in any language is a bad idea, even if it is allowed.](https://stackoverflow.com/questions/77552/id-is-a-bad-variable-name-in-python) – not_speshal Sep 20 '21 at 20:12
  • 1
    @randomer64 `int` is a valid variable name only in the technical sense here. It's a built-in type and should absolutely not be used in this context. – BTables Sep 20 '21 at 20:15