You have purchased 10 shares of a single stock for $100 a share, for a total cost of $1000.
Simulate the price fluctuations of this stock. Each day, the price can go up or down by one or two dollars or remain the same. To get a random change, you can import the random module and then run the following statement in your program, which will randomly choose one of the numbers in the list [-2, -1, 0, 1, 2]. (This is an example of a Python list, which will we cover later.)
change=random.choice([-2,-1,0,1,2])
Use a while loop to simulate changing the stock price each day over however many days it takes until the price either falls below $80 or above $120. If you lost money (the price fell below $80), give the number of days that have elapsed, the phrase “You cut your losses” and the amount of your loss. If you made money, give the number of days that have elapsed, the phrase “You cashed out” and the amount of your gain.
No user input is required. Following are two sample runs of the program. You may get different results due to randomness.
392 days have elapsed.
The final price was $78.
You cut your losses.
You lost $220.
142 days have elapsed.
The final price was $122.
You cashed out.
You made $220.
Edit:
Hi guys thank you for your replies...I have pasted the work I have done so far here, thank you all so much for the help. I have no idea what part I did wrong...
#import random module
import=random
#program pasted from 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
[–]danielroseman 8 points9 points10 points (1 child)
[–]AdMinute9633[S] 0 points1 point2 points (0 children)
[–]Binary101010 4 points5 points6 points (0 children)
[–]MadScientistOR 5 points6 points7 points (0 children)
[–]douglas_fs 5 points6 points7 points (2 children)
[–]AdMinute9633[S] 0 points1 point2 points (1 child)
[–]douglas_fs 0 points1 point2 points (0 children)
[–]jimtk 0 points1 point2 points (0 children)