Advertisement
Guest User

dicegame

a guest
Aug 29th, 2014
528
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.25 KB | None | 0 0
  1. import random
  2.  
  3. def addToRange(numList):
  4.     return [i + 1 for i in numList]
  5.  
  6. def convToDict(seq):
  7.     tempDict = {}
  8.     for i,item in enumerate(seq):
  9.         tempDict.update({i+1:item})
  10.     for key,value in tempDict.items():
  11.         tempDict[key] = 0
  12.     return tempDict
  13.  
  14. def counter(numSeq,rolls):#params: dictionary and the number of rolls
  15.     i = 1
  16.     while i < rolls+1:
  17.         a = random.randint(1,max(numSeq.keys()))
  18.         for value in numSeq.keys():
  19.             if a == value:
  20.                 numSeq[value] += 1
  21.         i += 1
  22.     return numSeq
  23.  
  24. def printResult(finalSeq):
  25.     finalSeq = sorted(finalSeq.items(),key=lambda x: x[1], reverse=True)
  26.     #returns a list with tuples
  27.     numOnce = []
  28.     numTwice = []
  29.     numMultiple = []
  30.     for i in finalSeq:
  31.         #loop through list
  32.         if i[1] == 2:
  33.             numTwice.append(i[0])
  34.         elif i[1] == 1:
  35.             numOnce.append(i[0])
  36.         elif  i[1]!= 1 and i[1]!= 2 and i[1] != 0:
  37.             print i[0]," appeared",i[1]," times."
  38.     if len(numTwice)>0:
  39.         print ",".join([str(item) for item in numTwice]), "appeared twice."
  40.     if len(numOnce)>0:
  41.         print ",".join([str(item) for item in numOnce]), "appeared once."
  42.    
  43.  
  44.  
  45. print "Welcome to dice roll!"
  46. print "-" * 20
  47.  
  48. while True:
  49.     while True:
  50.         numofSides = raw_input("Please choose number of sides on your dice: \n")
  51.         try :
  52.             if int(numofSides):
  53.                 numofSides = int(numofSides)
  54.                 break
  55.         except ValueError:
  56.             print ("Thought that was a number? Try again!\n")
  57.  
  58.     while True:
  59.         numofRolls = raw_input("Please choose number of dice rolls: \n")
  60.         try :
  61.             if int(numofSides):
  62.                 numofRolls = int(numofRolls)
  63.                 break
  64.         except ValueError:
  65.             print ("Thought that was a number? Try again!\n")
  66.                
  67.     numofSides = range(numofSides)
  68.     numofSides = addToRange(numofSides)
  69.     numofSides = convToDict(numofSides)
  70.     numofSides = counter(numofSides,numofRolls)
  71.     printResult(numofSides)
  72.  
  73.     keepPlay = raw_input("Play again? Y/N\n")
  74.     if keepPlay.lower().startswith("n"):
  75.         print "Thanks for playing! Press any key to quit.\n"
  76.         raw_input()
  77.         break
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement