After finishing my course on CodeAcademy, I wanted to write a program. Please provide opinions.
from random import randint
import sys, time, os
def clear(): #Usefull for clearing the terminal, should be cross-platform
os.system('cls' if os.name=='nt' else 'clear')
def initialize_variables(): #Self Described
global count, start, count_pos, initi_pos
count = 0
start = ""
count_pos = 2
initi_pos = 0
def open_file(): #Opens the options' file
global options_file
options_file = open("options.txt", "a+")
def close_file(): #Closes the option's file
global options_file
options_file.close()
#return options_file.closed #Debugging purposes
def initial_setup(): #
global options_file
global count_pos, initi_pos
options_file.seek(initi_pos, 0)
if options_file.read(1) == "": #Check the first value of the file, to determine if the user is running the Dice Simulator (DS from now on)
options_file.write("0") #If no value found, set it at default "0"
options_file.seek(initi_pos, 0) #Reposition cursor
if (options_file.read(1) == "0") : #If default value found, the user's running the program for the first time.
print "Welcome to the dice simulator"
print "It seems like you are here for the first time."
options_file.seek(count_pos, 0) #Reposition cursor
options_file.write(str(raw_input("After how many times you want to change the dice? "))+ "\n")
print "Done!",
for i in range(1,4):
time.sleep(0.5)
print ".",
options_file.seek(initi_pos, 0) #Reposition cursor to initi value
options_file.write("1") #Change it to our new default value
time.sleep(2)
clear() #Clear the window
else:
pass
def change_options():
global options_file
count_2file = int(raw_input("After how many rolls will you change the dice? "))
options_file.seek(count_pos,0)
options_file.write(str(count_2file))
clear()
def menu():
print "Dice Rolling Simulator |"
print "-----------------------/"
print "1. Start rolling"
print "2. Change options"
print "3. Quit"
choice = int(raw_input("Your selection is: "))
if choice == 1: #If first choice is selected
game() #Start the game
elif choice == 2: #If second choice is selected
change_options() #Change the options
menu() #Return to menu
elif choice == 3: #If third choice is selected
print "Application will now exit."
time.sleep(3)
sys.exit() #Kill the app.
else: #INVALID INPUT
print "Invalid input. Error code: 0x001" #Print the appropriate message
print "Application will now exit."
time.sleep(3)
sys.exit() #Kill the app
def game():
global start
x = int(raw_input("The size of the dice(s) is: "))
y = int(raw_input("How many times will you roll it? "))
start = raw_input("Roll the dice (y/n)? ")
while True:
if start == "y":
clear()
print roll_dice(x,y)
start = roll_again()
global count
count += 1
promtp_change(count)
elif start == "n":
clear() #Clear screan
print "Application will now exit."
time.sleep(2)
close_file() #Before exit make sure to close the file
sys.exit()
elif start != "y" and start != "n":
clear()
print "Invalid input. Error code: 0x001"
print "Application will now exit."
time.sleep(2)
close_file() #Before exit make sure to close the file
sys.exit()
###-###-###-###-###-###-###-###-###
def roll_dice(sides, num_rolls):
total_rolls = 0
roll_result = 0
result_sum = 0
for i in range(0, num_rolls):
roll_result = randint(1, sides)
print "Die #%d rolled %d" % (i+1, roll_result)
result_sum += roll_result
total_rolls += 1
print "Total of %d dice rolls is: %d" % (num_rolls, result_sum)
return None
def roll_again():
global start
start = raw_input("Roll again? ")
return start
def promtp_change(cnt):
global options_file, count_pos
options_file.seek(count_pos, 0)
pref_val_cnt = options_file.read(1) #The user's saved value for 'count'
if int(cnt) == int(pref_val_cnt): #If they are the same, prompt to change dice
temp_var_a = raw_input("Change dice? ")
if temp_var_a == "y": #If yes
clear()
game() #Restart the game, by calling it again
else:
pass #Elsewise continue
###*###*###*###*###*###*###*###*###
def main():
initialize_variables()
open_file()
initial_setup()
menu()
###*###*###*###*###*###*###*###*###
#----------------------Drop zone----------------------#
main()
#-----------------------ERRORS-EXPLAINED-----------------------#
# 0x001 : User's input was not expected.
# Possible reasons, most common:
# *Value not in possible choices
# *Expected str, input is int or vice-versa