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+')
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)
print "Money: \$" + str(money) + " High Score: \$" + str(hscore)
money = run(money)

raw_input()``````

have you tried making the stuff you get from the file an integer
That way it compares two integers not a string and an integer.

Hi. I cleaned up your code. I didn't put it in a class, but you really should consider it.

``````import random

def run(money):
global go
try:
bet = int(raw_input("\nPlace your bet: "))
if bet <= money and bet > 0:
money -= bet
money = roll(bet, money)
return money
elif bet <= 0:
go = False
return money
else:
print "You can't bet that much money!"
return money
except ValueError:
print "You must enter a number!"
return money

def roll(bet, money):
die = random.randrange(1, 100)
if die >= 50:
if die >= 90:
print "Jackpot! You rolled " + str(die)
money = money + (bet * 5)
else:
print "Winner! You rolled " + str(die)
money = money + (bet * 2)
else:
print "Lose! You rolled " + str(die)
return money

def start():
global go
go = True
money = 500
try:
except:
go = False
print "Welcome to Super Ultimate Roll-Master 3000!\nRolls are 1-100: 0-49 (Lose), 50-90 (Bet*2), 91-100 (Bet*5)\nEnter a bet of \$0 to quit.\n"
while go:
if money > highScore:
highScore = money
print "Money: \$" + str(money) + " High Score: \$" + str(highScore)
money = run(money)
try:
scoreFile = open('highscores.txt', 'w')
scoreFile.write(str(money))
scoreFile.close()
except:
print "Problem writing 'highscores.txt'"

if __name__ == "__main__":
start()``````

Your error was probably a combination of little things. For example, you didn't close your files after accessing them, and you tried to compare a string to an int, like paul said, which always returned False.

Thanks Fuse (and everyone else).

I am going through the tutorials and using the features as they are presented to me. I happen to be on Modules at the moment and I think Classes are next- or soon.

I tried converting the file contents to int but I might have did it in the wrong place since it still didn't work.

I will take a look at your code Fuse and analyze it.

Thanks again.

Might I suggest going through the first few chapter of this e-book? Up to chapter 5 (file IO), at least. It covers things like closing your files after access and stuff. Good luck! :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.