Python Forum
Generalized Dice refactoring
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Generalized Dice refactoring
#1
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).

#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
Reply
#2
(Feb-04-2019, 09:20 PM)Beginner_coder123 Wrote: number = random.randint(0,len(die)-1)
This is the same, and clearer: number = random.randint(len(die))
But this is even clearer: number = random.choice(die)

(Feb-04-2019, 09:20 PM)Beginner_coder123 Wrote: ending_conditions = ['stop','Stop','quit','Quit']
If you call .lower() on the input, you only need ending_conditions = ['stop', 'quit']. As an added bonus, you also properly handle weird things like "sToP", or "QUIT".

(Feb-04-2019, 09:20 PM)Beginner_coder123 Wrote: while end == False:
That's kind of gross, and would look nicer as while not end:.

(Feb-04-2019, 09:20 PM)Beginner_coder123 Wrote: if not(inpu in ending_conditions):
I'd prefer to see the "not" next to the "in", like such: if inpu not in ending_conditions:

The functions quantitymatrix and deeper are the same. One of them can just be deleted, and you can call the other instead.

Aside from that, what is it you're trying to refactor?
Reply
#3
(Feb-04-2019, 10:03 PM)nilamo Wrote:
(Feb-04-2019, 09:20 PM)Beginner_coder123 Wrote: number = random.randint(0,len(die)-1)
This is the same, and clearer: number = random.randint(len(die))
But this is even clearer: number = random.choice(die)

(Feb-04-2019, 09:20 PM)Beginner_coder123 Wrote: ending_conditions = ['stop','Stop','quit','Quit']
If you call .lower() on the input, you only need ending_conditions = ['stop', 'quit']. As an added bonus, you also properly handle weird things like "sToP", or "QUIT".

(Feb-04-2019, 09:20 PM)Beginner_coder123 Wrote: while end == False:
That's kind of gross, and would look nicer as while not end:.

(Feb-04-2019, 09:20 PM)Beginner_coder123 Wrote: if not(inpu in ending_conditions):
I'd prefer to see the "not" next to the "in", like such: if inpu not in ending_conditions:

The functions quantitymatrix and deeper are the same. One of them can just be deleted, and you can call the other instead.

Aside from that, what is it you're trying to refactor?

Hello, ah appreciated the help. I would like to refactor my code into something more efficient. With the use of memoization, for example. Some higher order functions if possible. But also to make the code look a little bit clearer. What's the best way for me to do this?
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  refactoring kerzol81 8 3,432 Nov-02-2019, 09:49 AM
Last Post: Gribouillis
  Python Connect 4 refactoring Beginner_coder123 6 3,729 Oct-29-2018, 05:30 PM
Last Post: Beginner_coder123

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020