Okay. So I'm writing this snakes and ladders game, and I want to be able to save the scores, but the try/except method seems to be giving me trouble. I feel like it has something to do with the fact that I'm not writing, just reading. But I'm not sure where, or even how, to do this correctly. Any thoughts?
import random
def create_board():
lst1 = [100, 99, 98, 97, 96, 95, 94, 93, 92, 91]
lst2 = [90, 89, 88, 87, 86, 85, 84, 83, 82, 81]
lst3 = [80, 79, 78, 77, 76, 75, 74, 73, 72, 71]
lst4 = [70, 69, 68, 67, 66, 65, 64, 63, 62, 61]
lst5 = [60, 59, 58, 57, 56, 55, 54, 53, 52, 51]
lst6 = [50, 49, 48, 47, 46, 45, 44, 43, 42, 41]
lst7 = [40, 39, 38, 37, 36, 35, 34, 33, 32, 31]
lst8 = [30, 29, 28, 27, 26, 25, 24, 23, 22, 21]
lst9 = [20, 19, 18, 17, 16, 15, 14, 13, 12, 11]
lst10 = [10, 9, 8, 7, 6, 5, 4, 3, 2, 1]
all_lst = lst1 + lst2 + lst3 + lst4 + lst5 + lst6 + lst7 + lst8 + lst9 + lst10
return all_lst
def print_board(board):
x = 0
print("-" * 50)
print("|", end="")
for number in board:
print(number, end="|")
x +=1
if x == 10:
print()
print("-" * 50)
x = 0
def snakes_ladders(board):
board[board.index(1)] = "L1"
board[board.index(15)] = "S1"
board[board.index(19)] = "L2"
board[board.index(33)] = "L1"
board[board.index(45)] = "S1"
board[board.index(48)] = "S2"
board[board.index(69)] = "L3"
board[board.index(75)] = "L2"
board[board.index(78)] = "S3"
board[board.index(82)] = "S2"
board[board.index(85)] = "L3"
board[board.index(98)] = "S3"
def roll_die():
roll1 = random.randrange(1,7)
total = (roll1)
return total
def exit_program():
exit()
def check_snakes(roll_count):
if roll_count == 98:
roll_count = 78
elif roll_count == 82:
roll_count = 48
elif roll_count == 45:
roll_count = 15
return roll_count
def check_ladders(roll_count):
if roll_count == 1:
roll_count = 33
elif roll_count == 19:
roll_count = 75
elif roll_count == 69:
roll_count = 85
return roll_count
def save_scores(score):
try:
scorefile = open('scores.txt', 'r')
scores = scorefile.readlines()
entry = str(score) + "\n"
scores.append(entry)
scorefile.close()
tempo = []
for entry in scores:
tempo.append(int(entry))
tempo.sort()
new_scores = []
for entry in tempo:
entry1 = str(entry) + "\n"
newscores.append(entry1)
newscores = newscores[:5]
scorefile.writelines(new_scores)
scorefile.close
except(IOError):
print("Could not open file for reading")
# main
score = 0
temp = 100
roll_count = 0
check_snakes(roll_count)
check_ladders(roll_count)
board = create_board()
snakes_ladders(board)
save_scores(score)
print_board(board)
while True:
roll = roll_die()
roll_quit = input("Type 'r' to roll die, or type 'e' to quit:\n>>>")
if roll_quit == "r":
score += 1
print("You rolled a ", roll,"!")
if roll_count + roll > 100:
print("That roll is too high")
continue
elif roll_count + roll == 1:
score -= 11
elif roll_count + roll == 19:
score -= 11
elif roll_count + roll == 69:
score -= 11
elif roll_count + roll == 100:
print("You win! With a score of",score)
break
elif roll_quit == "e":
break
else:
print("That isn't an option.")
continue
# use negative indexing for the roll number, [-1] = 1
print("Current score:",score)
board[-roll_count] = temp
roll_count = (roll_count) + (roll)
roll_count = check_snakes(roll_count)
roll_count = check_ladders(roll_count)
temp = board[-roll_count]
board[-roll_count] = "X"
(print_board(board))