Advertisement
makispaiktis

Roulette

Dec 16th, 2020 (edited)
282
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.69 KB | None | 0 0
  1. from random import randrange
  2. from matplotlib import pyplot as plt
  3.  
  4. # Function 1
  5. def doIContinue(answer):
  6.     flag = True
  7.     if answer.lower() == "no":
  8.         print("So, you want to stop. Nice game!")
  9.         flag = False
  10.     elif answer.lower() == "yes" or answer.lower() == "sure":
  11.         print("Wise choice, maybe not. Let's continue to the next bet!")
  12.         flag = True
  13.     else:
  14.         print("You did not answer neither 'yes' nor 'no'. I will suppose 'yes', next time be more careful.")
  15.         flag = True
  16.     print()
  17.     return flag
  18.  
  19. # **********************************************************************************************************
  20. # ********************************** MAIN FUNCTION *********************************************************
  21. # **********************************************************************************************************
  22. print()
  23. print("Welcome to the Roulette game. Rules are simple. You can only bet in red color")
  24. wins = 0
  25. tries = 0
  26. stats01 = []
  27. bets = []
  28. money = int(input("Money here: "))
  29. while money < 0:
  30.     money = int(input("Money here: "))
  31. initialMoney = money
  32. answer = "yes"
  33.  
  34. # I will simulate the simulation with another way: red will be if my number is between 1 and 18,
  35. # black will be if my number is between 19 and 36 and "0" will be number 37
  36. while money > 0 and answer != "no":
  37.     answer = input("Do we continue? ")
  38.     flag = doIContinue(answer)
  39.     if flag == True:            # That means we continue
  40.         tries += 1
  41.         print("************************ Try ", tries, "************************")
  42.         bet = int(input("Put your bet here: "))
  43.         while bet > money:
  44.             bet = int(input("Put your bet here: "))
  45.         bets.append(bet)
  46.         # Now, we have the bet that user put in. This bet will be substracted from sum = money
  47.         money = money - bet
  48.         # We will suppose that our user ALWAYS bets 'red', so we want the random generated number to be
  49.         # between 1 and 18
  50.         randomNumber = randrange(0, 37)
  51.         print("Bet =", bet, ", randomNumber =", randomNumber)
  52.         if randomNumber >= 1 and randomNumber <= 18:
  53.             wins += 1
  54.             money += 2 * bet
  55.             stats01.append(1)
  56.             print("You won, because 1 <=", randomNumber, "<= 18")
  57.             print("Current money: ", money)
  58.         elif randomNumber >= 19 and randomNumber <= 36:
  59.             stats01.append(0)
  60.             print("You lost, because 19 <=", randomNumber, "<= 36")
  61.             print("Current money: ", money)
  62.         else:
  63.             stats01.append(0)
  64.             print("You lost, because randomNumber = 0 ----> green color")
  65.             print("Current money: ", money)
  66.         print("*******************************************************")
  67.         print()
  68.  
  69. # Now, the basic process has finished. The remaining stuff is to show the stats in the user and the plot of his luck
  70. print()
  71. print("*******************************************************")
  72. print("Here, I will present you the stats of your gameplay.")
  73. print("Tries =", tries, ", wins =", wins, ", losses =", tries - wins)
  74. winPercentage = wins / tries
  75. winPercentage = round(10000 * winPercentage) / 10000
  76. print("Win percentage  = ", 100 * winPercentage, "%")
  77. print()
  78. gain = money - initialMoney
  79. gainPercentage = gain / money
  80. gainPercentage = round(10000 * gainPercentage) / 10000
  81. print("Initial money =", initialMoney, ", Final Money =", money, "(", 100 * gainPercentage, "%)")
  82. # Plotting our data
  83. plt.plot(stats01, 'ro')
  84. plt.title("0 = Loss, 1 = Win")
  85. plt.xlabel("Rounds")
  86. plt.ylabel("Win/Loss")
  87. plt.show()
  88. plt.plot(bets, 'bx')
  89. plt.title("Bets value per round")
  90. plt.xlabel("Rounds")
  91. plt.ylabel("Bet")
  92. plt.show()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement