So I have a simple dice program that lets the user make bets. If their current money pool is larger than the money pool in highscores.txt it will write the new score to the file.
The program works as intended except for the high scores. When I try to write the new score to the file, it writes a huge piece of code which looks like it is part of one of the modules.
I think it might have something to do with reading the file as a string and treating it as an int, but I'm not positive.
All of the file code is at the end and the beginning.
Here is the code:
import random
money = 500
hs = open('highscores.txt', 'r+')
hscore = hs.read()
bet, trybet = 0,0
def run(money):
try:
trybet = int(raw_input("\nPlace your bet: "))
if trybet <= money and trybet > 0:
bet=trybet
money = money - bet
money = roll(bet,money)
return money
else:
print "You can't bet that much money!"
return money
except ValueError:
print "You must enter a number!"
return money
def rand(x,y):
return random.randrange(x,y)
def roll(bet,money):
die = rand(1,100)
if die >= 50:
if die <= 90:
print "Winner! You rolled " + str(die)
money = money + (bet * 2)
else:
print "Jackpot! You rolled " + str(die)
money = money + (bet * 5)
else:
print "Lose! You rolled " + str(die)
return money
print "Welcome to Super Ultimate Roll-Master 3000!"
print "\nRolls are 1-100: 0-49 (Lose), 50-90 (Bet*2), 91-100 (Bet*5)\n"
while True:
if money > hscore:
hs.seek(0)
s = str(money)
hs.write(s)
hscore = hs.read()
print "Money: $" + str(money) + " High Score: $" + str(hscore)
money = run(money)
raw_input()