import numpy as np
import pandas as pd
import random
import matplotlib.pyplot as plt
def gamblers_ruin ():
gambling_money = 50
gambling_goal =100
gambling_simulations = []
while gambling_money in range (1, gambling_goal):
bet_size =1
w_or_1= random.randrange(-1, 2, step=2)
gambling_money += bet_size * w_or_1
gambling_simulations.append(gambling_money)
return gambling_simulations
plt.plot(gamblers_ruin())
plt.yticks(np.arange(-20,120,10))
plt.axhline(y=0,color='r',linestyle='-')
plt.axhline(y=100,color='black',linsestyle='-')
plt.xlabel('number of bets')
plt.ylabel('winings')
plt.title('gambling simulations')
plt.show()
As you can see, code pasted into a post can’t be understood unless you follow this advice.
(Don’t worry. Everyone needs to be told this at least once.)
Hi !
I think you have to unindent the lines where you define your figure. In the code you sent, all the plt.xxx
calls belong to the definition of gamblers_ruin()
, but they’re never executed because return
is called earlier.
I also assume that the return
should be unindented to be out of the while
loop, otherwise this loop never iterates more than once.
Thanks for quoting the code nicely. Now you’ve done that, it was easy for me to copy it and debug it.
If you correct the remaining problems with the indenting as @WeisLeDocto suggests, and the spelling of linestyle
(line 25 in my copy), the code will be correct and you should find it runs. It ran for me on Windows, using Idle, and Python 3.12.
If there’s still a problem, tell us in what environment you are running it (PC, Jupyter, CoLab) and whether you get any error messages. It’s best if you copy and paste error messages and quote them like code. If that’s fixed it for you, let us know too.