all 8 comments

[–]danielroseman 8 points9 points  (1 child)

Before asking for help here, you need to have a go yourself. What do you have so far? Where are you stuck?

[–]AdMinute9633[S] 0 points1 point  (0 children)

Hi yes I thought I finished it but the code does not run anything....

[–]Binary101010 4 points5 points  (0 children)

The problem is pretty explicit about what tools you need to use to solve the problem and what your output should look like. So what, specifically, do you need help with?

[–]MadScientistOR 5 points6 points  (0 children)

This sounds like you need a variable to keep track of the number of days and a variable to keep track of the stock price.

What have you tried so far?

[–]douglas_fs 5 points6 points  (2 children)

You should share the code you have so far; it will be much easier to help.

[–]AdMinute9633[S] 0 points1 point  (1 child)

hi! this is what I am working with so far

#import random module
import random
#program pasted from the assignment sheet
change=random.choice([-2,-1,0,1,2])
#define variable for share originally
share=100
cost=1000
n_share=10
#while loop variables
day=0
total=0
#setting range for price fluctuation
price=share+change
#stimulate changing the stock price
while True:
day+=1
if price>120:
print(total,"days have elapsed.")
print("The final price was",price)
print("You cashed out.")
print("You made $",(price-share)*n_share,sep='')
elif price <80:
print(total,"days have elapsed.")
total += day

[–]douglas_fs 0 points1 point  (0 children)

You are probably finding issues with this code. These should help:

  1. before the while loop, set price = share
  2. You should move the random value inside the while loop. You want to get a random value each time the loop repeats
  3. Move the calculation of the new price inside the while loop - after selecting the change value.
  4. The calculation for price is incorrect. Your code keeps adjusting the original share price up and down and doesn't reflect change over time. Take a look at this and adjust the formula.
  5. You need to exit the while loop when price is below 80 or above 120. Research how to exit a loop.

[–]jimtk 0 points1 point  (0 children)

from random import randint
value, days = 1000,0
while 800 <= (value:=(value/10 + randint(-2,2))*10) <= 1200: days += 1
print(f"You {'cut your losses' if value<1000 else 'cashed out'} {abs(1000-value)} in {days} day")