I'm trying to calculate the number of rolls it takes to go broke, and the amount of rolls that would have left you with the most money. The program is split into several functions outside of main (not my choice) so that makes it more difficult for me.
I'm very new to python, and this is an exercise for school. I'm just not really sure where to go from here, and I realize I'm probably doing some of this wrong. Here's the code I have so far:
import random
def displayHeader(funds):
print ("--------------------------")
print ("--------------------------")
print ("- Lucky Sevens -")
print ("--------------------------")
print ("--------------------------")
funds = int(input("How many dollars do you have? "))
def rollDie(newFunds):
#this function is supposed to simulate the roll of two die and return results
while funds > 0:
diceRoll = random.randint(1,6)
totalRoll = (diceRoll diceRoll)
if totalRoll == 7:
funds = funds 4
else:
funds = funds - 1
if funds == 0:
newFunds = funds
def displayResults():
#this function is supposed to display the final results.
#the number of rolls, the number of rolls you should have stopped at, and the max amount of money you would have had.
def main():
#everything gathered from the last function would be printed here.
main()
CodePudding user response:
import random
maxmoney = []
minmoney = []
def displayHeader():
print ("--------------------------")
print ("--------------------------")
print ("- Lucky Sevens -")
print ("--------------------------")
print ("--------------------------")
funds = int(input("How many dollars do you have? "))
return funds
def rollDie():
#this function is supposed to simulate the roll of two die and return results
funds = displayHeader()
while funds > 0:
diceRoll1 = random.randint(1,6)
diceRoll2 = random.randint(1,6)
totalRoll = (diceRoll1 diceRoll2)
if totalRoll == 7:
funds = funds 4
maxmoney.append(funds)
else:
funds = funds - 1
minmoney.append(funds)
def displayResults():
#this function is supposed to display the final results.
#the number of rolls, the number of rolls you should have stopped at, and the max amount of money you would have had.
rollDie()
numOfRolls = len(maxmoney) len(minmoney)
numOfRolls2Stop = (len(maxmoney) - 1 - maxmoney[::-1].index(max(maxmoney))) (len(minmoney) - 1 - minmoney[::-1].index(max(maxmoney)-1)) 1 if maxmoney and minmoney else 0
maxAmount = max(maxmoney) if maxmoney else 0
return numOfRolls, numOfRolls2Stop, maxAmount
def main():
#everything gathered from the last function would be printed here.
a, b, c = displayResults()
print('The number of total rolls is : ' str(a))
print("The number of rolls you should've stopped at is: " str(b))
print("The maximun amount of money you would've had is: $" str(c))
main()
CodePudding user response:
Your program use variables that can only be accessed in certain scopes, so I think that the most recommended thing is that you use a class.
displayHeader
input method it could stop the execution of the program since if you do not introduce a numerical value it will raises an exception called ValueError
, if this does not help you much, I advise you to read the code carefully and add missing variables such as the input amount and the final amount and others...
class rollDiceGame():
def __init__(self):
self.funds = 0
self.diceRollCount = 0
def displayHeader(self):
print ("--------------------------")
print ("--------------------------")
print ("- Lucky Sevens -")
print ("--------------------------")
print ("--------------------------")
self.funds = int(input("How many dollars do you have? "))
def rollDice(self):
while self.funds > 0:
diceRoll = random.randint(1,6)
totalRoll = (diceRoll diceRoll)
self.diceRollCount = 1
if totalRoll == 7:
self.funds = 4
else:
self.funds -= 1
def displayResult(self):
print('Roll count %d' % (self.diceRollCount))
print('Current funds %d' % (self.funds))
def main():
test = RollDiceGame()
test.displayHeader()
test.rollDice()
test.displayResult()
main()