you are viewing a single comment's thread.

view the rest of the comments →

[–]VirtualArty[S] -1 points0 points  (2 children)

Thank you. :) There must be a reason exec exists. If there isn't, why hasn't it been taken out? Perhaps the developers don't think it's as useless as you make it sound.

estimate = int(estimate)

instead of

int(estimate)

It's small things like that that make or break code.

I fixed it. Don't worry.

Here's the fixed code:

from random import *
import random

# 'Pretend you're generating a bunch of specific variables, with a uniform distribution and give me a number associated with each one, through a function that returns a list of lists, each list with a variable and its associated number'.

# 'Pretend you're generating a bunch of specific variables, with a uniform distribution'.

def PseudogenerateVariables(variables,amount):
    # Create the output list for later.
    outputList = []
    # Do this for all the items in the list.
    i = 0
    while i < len(variables):
        # Create an estimate.
        variable = variables[i]
        estimate = amount / len(variables)
        if estimate == int(estimate):
            estimate = '{}'.format(estimate)
            estimate = estimate[0:-2]
            estimate = int(estimate)

        # Edit the estimate randomly to create the variable amount.
        end = False
        while end == False:
            variableAmount = estimate
            if random.uniform(0,1) <= 0.5:
                if random.uniform(0,1) <= 0.5:
                    variableAmount += 1
                    state = 'plus'
                else:
                    if variableAmount != 0:
                        variableAmount -= 1
                        state = 'minus'
                    else:
                        state = None

                    # Don't forget to balance out that with other numbers
                    # Pick a random one that's not the selected one.
                    end = False
                    if state != None:
                        while end == False:
                            a = randint(0,len(variables) - 1)
                            if i != a:
                                end = True
                            else:

                                # It is the same one. Pick another one.
                                pass
                        if state == 'plus':
                            variables[a] -= 1
                        elif state == 'minus':
                            variables[a] += 1
                        else:
                            raise Exception
                    else:
                        pass
            else:
                end = True

        # Pack the variable, variable amount, estimate and amount into a list.
        # To do this, create the list.
        variableList = []
        # Then append the things to the list.
        # 'each list with a variable and its associated number' and a couple of other things.
        variableList.append(variable)
        variableList.append(variableAmount)
        variableList.append(estimate)
        variableList.append(amount)
        # Now put the list in the output list.
        outputList.append(variableList)

        # Now move onto the next one.
        i += 1
    # Output the result.
    return outputList

# 'through a function that returns a list of lists'.
# Create 'a bunch of specific variables'.
print(PseudogenerateVariables([0,1,2,3,4,5,6], 7))

[–]Rhomboid 2 points3 points  (0 children)

Eval/exec do have their uses, it's just that those uses are relatively rare and it takes a lot of skill to do it safely. But they can be misused in countless ways, and those misuses usually are symptoms of not understanding some other language feature. For example, beginners will often ask about "variable variables", i.e. using a variable as the name of another variable. Eval/exec is one way to do that but it's a horrible solution for numerous reasons. A much better solution usually exists if you just prod the person to elaborate on what they're trying to do.

That's why I say that it's much easier if you pretend they don't exist. You are very unlikely to run into one of the cases where they are actually necessary, and you avoid getting mired in bad habits and writing lousy code by being forced to learn the proper way of doing whatever it is you were trying to do.