HiveBrain v1.2.0
Get Started
← Back to all entries
patternpythonModerate

Interactive dice simulator

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
simulatorinteractivedice

Problem

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

Solution

def initialize_variables(): #Self Described
    global count, start, count_pos, initi_pos
    count = 0 
    start = ""
    count_pos = 2
    initi_pos = 0


-
Do not use global variables, never. Functions are made to process input and give output. Your functions do not take input parameter. Change your code so that the functions will take input and there will not be global variables (or few of them).

-
You have a nonsense comment: #Self Described. Comments like this one should be avoided because they just confuse the reader.

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


These functions are meaningless because you can use:

with open('options.txt') as f:
    #do something


that will also handle closing automatically.

Error code: 0x001


What? You should print a human readable error message.

Please avoid:

print "Dice Rolling Simulator |"
print "-----------------------/"
print "1. Start rolling"
print "2. Change options"
print "3. Quit"


instead for better clarity you should print a multiline message declared at the start of the file:

MENU = """Dice Rolling Simulator |"
"-----------------------/"
"1. Start rolling"
"2. Change options"
"3. Quit"""

# other code

print MENU

Code Snippets

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
with open('options.txt') as f:
    #do something
Error code: 0x001
print "Dice Rolling Simulator |"
print "-----------------------/"
print "1. Start rolling"
print "2. Change options"
print "3. Quit"

Context

StackExchange Code Review Q#75636, answer score: 12

Revisions (0)

No revisions yet.