Feb-04-2019, 09:20 PM

Hello everyone, I need some help with refactoring my code for an assignment. The assignment is as following: a. Solve problem 205 as a probabilistic experiment. This will not get you to 7 digits precision, but 2 to 3 digits should be possible.

b. Adapt the code for part a such that it can handle two sets of 'generalized dice'. Each set consists of a number of equal objects that generate numbers from a finite set with an arbitrary probability distribution. You only need to hand in the code for part b (not for a).

b. Adapt the code for part a such that it can handle two sets of 'generalized dice'. Each set consists of a number of equal objects that generate numbers from a finite set with an arbitrary probability distribution. You only need to hand in the code for part b (not for a).

#Assignment import time import random def roll(die): number = random.randint(0,len(die)-1) b = die[number] return b Die1 = [1,2,3,4] Die2 = [1,2,3,4,5,6] #num lists def inptchacc(string): ending_conditions = ['stop','Stop','quit','Quit'] end = False inpu = input(string) while end == False: if not(inpu in ending_conditions): try: retr = int(inpu) return retr except: string = 'invalid input please try again' inpu = input('invalid input please try again ') else: stop = 'stop' return stop def quantitymatrix(IDie): stringDie = 'how often would you like this Die to occur?' list = [] Adding = True while Adding: print('the Die in question is '+ str(IDie)) toadd = inptchacc(string) if toadd != 'stop': list.append(toadd) else: Adding = False return list def deeper(IDie): stringDie = 'what number would you like to add to the new face of the Die? (to end the die please type "stop or Stop or quit or Quit to finish the DIE" )' list = [] Adding = True while Adding: print('The Die ' + IDie + ' is currently ' + str(list) ) toadd = inptchacc(stringDie) if toadd != 'stop': list.append(toadd) else: Adding = False return list def chance_overlap(dielist,Dielistcount): highnumber = 100000 counter = (len(dielist))*[0] chance = (len(dielist))*[0] for n in range(highnumber): dieres = len(Dielistcount)*[0] for dienumber in range(len(dielist)): for diecount in range(Dielistcount[dienumber]): dieres[dienumber] += roll(dielist[dienumber]) for dienumber2 in range(len(dielist)): if max(dieres) == dieres[dienumber2] and dieres.count(max(dieres)) == 1: counter[dienumber2] += 1 for chanceper in range(len(counter)): chance[chanceper] = counter[chanceper]/highnumber chance[chanceper] = str(chance[chanceper]) + '% for die' + str(chanceper+1) return chance def suckmypp(counterq): string1 = 'adding the amount of the die '+ str(counterq+1) firstq = True while firstq: suckmypp2 = inptchacc(string1) if suckmypp2 != 'stop': firstq = False return suckmypp2 Dielist1 = [Die1,Die2] diecount = [9,6] chance = chance_overlap(Dielist1,diecount) print(chance) Doing = True counter = 0 while Doing: Dielist2 = [] adding = True while adding: counter += 1 addQ = input('to stop type S and enter otherwise any characters and enter will add another die') if addQ != 'S': notdone = True while notdone: dietoadd = deeper('Die' + str(counter)) if len(dietoadd) >= 1: Dielist2.append(dietoadd) notdone = False else: print('die is empty not added') else: adding = False quantity = True counterq = 0 Qlist = [] print(Qlist) print(len(Dielist2)) while quantity: Qlist.append(suckmypp(counterq)) counterq += 1 if counterq == (len(Dielist2)): quantity = False print(Dielist2) print(Qlist) chance2 = chance_overlap(Dielist2,Qlist) Doing = False print(chance2)How can I refactor this in a more "efficient" way? I tried everything, but I keep breaking my code. Please don't mind the variable names, certain callback names etc. They are satire