Python Forum
Why am I getting list elements < 0 ?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Why am I getting list elements < 0 ?
#1
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?
Reply


Messages In This Thread
Why am I getting list elements < 0 ? - by Mark17 - Aug-23-2021, 02:10 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  unable to remove all elements from list based on a condition sg_python 3 451 Jan-27-2024, 04:03 PM
Last Post: deanhystad
Question mypy unable to analyse types of tuple elements in a list comprehension tomciodev 1 484 Oct-17-2023, 09:46 AM
Last Post: tomciodev
  Checking if a string contains all or any elements of a list k1llcod3 1 1,108 Jan-29-2023, 04:34 AM
Last Post: deanhystad
  How to change the datatype of list elements? mHosseinDS86 9 2,005 Aug-24-2022, 05:26 PM
Last Post: deanhystad
  ValueError: Length mismatch: Expected axis has 8 elements, new values have 1 elements ilknurg 1 5,157 May-17-2022, 11:38 AM
Last Post: Larz60+
  Looping through nested elements and updating the original list Alex_James 3 2,141 Aug-19-2021, 12:05 PM
Last Post: Alex_James
  Extracting Elements From A Website List knight2000 2 2,284 Jul-20-2021, 10:38 AM
Last Post: knight2000
  Make Groups with the List Elements quest 2 1,982 Jul-11-2021, 09:58 AM
Last Post: perfringo
  I cannot delete and the elements from the list quest 4 2,988 May-11-2021, 12:01 PM
Last Post: perfringo
  List of lists - merge sublists with common elements medatib531 1 3,414 May-09-2021, 07:49 AM
Last Post: Gribouillis

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020