Python Forum
Why am I getting list elements < 0 ? - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Why am I getting list elements < 0 ? (/thread-34701.html)



Why am I getting list elements < 0 ? - Mark17 - Aug-23-2021

Hi all!

Here's some code:

price_chg = np.arange(-10,11,2)

stock_prices = [100]

for i in range(10001):
    if (stock_prices[-1] + np.random.choice(price_chg)) < 0:
        stock_prices.append(0)
        continue
    else:
        stock_prices.append(stock_prices[-1] + np.random.choice(price_chg))
Why is the if block failing to keep negative integers out of the list?


RE: Why am I getting list elements < 0 ? - ibreeden - Aug-23-2021

(Aug-23-2021, 02:10 PM)Mark17 Wrote: Why is the if block failing to keep negative integers out of the list?
Because you are generating a new random price in the "else" block which can again be negative.
Apparently you mean to do this:
price_chg = np.arange(-10,11,2)
 
stock_prices = [100]
 
for i in range(10001):
    random_price = stock_prices.append(stock_prices[-1] + np.random.choice(price_chg))
    if random_price < 0:
        stock_prices.append(0)
    else:
        stock_prices.append(random_price)



RE: Why am I getting list elements < 0 ? - Mark17 - Aug-23-2021

(Aug-23-2021, 02:44 PM)ibreeden Wrote:
(Aug-23-2021, 02:10 PM)Mark17 Wrote: Why is the if block failing to keep negative integers out of the list?
Because you are generating a new random price in the "else" block which can again be negative.
Apparently you mean to do this:
price_chg = np.arange(-10,11,2)
 
stock_prices = [100]
 
for i in range(10001):
    random_price = stock_prices.append(stock_prices[-1] + np.random.choice(price_chg))
    if random_price < 0:
        stock_prices.append(0)
    else:
        stock_prices.append(random_price)

When will the "stupid mistakes" end? Rhetorical question. :)

Thank you!


RE: Why am I getting list elements < 0 ? - deanhystad - Aug-23-2021

max() may be a better choice.
import random
price = 10
stock_prices = [price]
for _ in range(11):
    price = max(0, price + random.randrange(-10, 11, 2))
    stock_prices.append(price)

print(stock_prices)



RE: Why am I getting list elements < 0 ? - naughtyCat - Aug-24-2021

One line code
import random
stock_prices = [10] + [max(0, 10+random.randrange(-10, 11, 2)) for _ in range(11)]



RE: Why am I getting list elements < 0 ? - deanhystad - Aug-24-2021

Not the same result. +/-10 is the range of daily fluctuation, not the range of stock_prices.


RE: Why am I getting list elements < 0 ? - naughtyCat - Aug-24-2021

(Aug-24-2021, 08:31 AM)deanhystad Wrote: Not the same result. +/-10 is the range of daily fluctuation, not the range of stock_prices.

two line
import random
price = 10
stock_prices = [price] + [max(0, price+random.randrange(-10, 11, 2)) for _ in range(11)]



RE: Why am I getting list elements < 0 ? - deanhystad - Aug-24-2021

Still not right. The price fluctuates from the previous day, not the first day. I don't think this can be done with a comprehension because there is no mechanism for remembering what the price was yesterday.


RE: Why am I getting list elements < 0 ? - naughtyCat - Aug-26-2021

(Aug-24-2021, 03:32 PM)deanhystad Wrote: Still not right. The price fluctuates from the previous day, not the first day. I don't think this can be done with a comprehension because there is no mechanism for remembering what the price was yesterday.

ya, you are right, I am learned, thanks