Line by line analysis
Use four spaces to indent python code. Never use tabs.
from random import randint from sys import exit import os
It is a good practice to do top-level imports (without importing module members). This way it's easier to understand what package the methods come from.
os.system('clear') print "Welcome to the dice rolling simulator!" raw_input("Press enter to begin.") total = 0 completed = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
Use list comprehensions.
def roll():
Roll method is confusing. You expect it to roll a die, but it processes iterations of the game. A while loop would be more explicit here.
os.system('clear') die1 = randint(1, 6) die2 = randint(1, 6) global total
Global variables are no-no. Never.
total = die1 + die2 storetotal() print "Die 1: %d \nDie 2: %d \nTotal: %d." % (die1, die2, total) print "\n\nRoll again?" roll_again = raw_input("Press enter to roll again, type 'stats' to view scores, or 'quit' to exit.\n> ")
You're exceeding recommended 80 column length limit.
if roll_again == "": roll()
See how I changed this block in a code below.
elif roll_again == "stats": stats() elif roll_again == "quit": exit(0)
Return from the method is sufficient here.
else: print "I don't know what that means so you get to roll again." raw_input("> ") roll() def stats(): global total print "2s: %d \n3s: %d \n4s: %d \n5s: %d \n6s: %d \n7s: %d \n8s: %d" % (completed[0], completed[1], completed[2], completed[3], completed[4], completed[5], completed[6]) print "9s: %d \n10s: %d \n11s: %d \n12s: %d""" % (completed[7], completed[8], completed[9], completed[10])
Indentation here is completely messed up.
raw_input("") roll() def storetotal():
PEP8: store_total.
if total == 2: completed[0] += 1 elif total == 3: completed[1] += 1 elif total == 4: completed[2] += 1 elif total == 5: completed[3] += 1 elif total == 6: completed[4] += 1 elif total == 7: completed[5] += 1 elif total == 8: completed[6] += 1 elif total == 9: completed[7] += 1 elif total == 10: completed[8] += 1 elif total == 11: completed[9] += 1 else: completed[10] += 1 roll()
Improved code
I simplified your roll method to roll2d6 and moved all the other code into main.
import random import os def roll2d6(): die1 = random.randint(1, 6) die2 = random.randint(1, 6) return die1 + die2 def display_stats(stats): for i, result in enumerate(stats): print "%ds: %d" % (i + 2, result) def update_stats(total, stats): stats[total - 2] += 1 return stats def main(): stats = [0 for _ in xrange(10)] os.system('clear') print "Welcome to the dice rolling simulator!" raw_input("Press enter to begin.") while True: os.system('clear') total = roll2d6() stats = update_stats(total, stats) print "Die 1: %d \nDie 2: %d \nTotal: %d." % (die1, die2, total) print "\n\nRoll again?" roll_again = raw_input(( "Press enter to roll again, type 'stats' to view scores, " "or 'quit' to exit.\n> ")) if roll_again.lower() == "stats": display_stats(stats) elif roll_again.lower() == "quit": break elif roll_again.lower() != "": print "I don't know what that means so you get to roll again." if __name__ == "__main__": main()