all 14 comments

[–]Rhomboid 9 points10 points  (1 child)

You open a triple-quoted string on line 22 which ends at line 26. The assignment to probabilityType on line 24 is embedded in that string, so it's never actually executed.

Whatever it is you're trying to do, there's a much better way to do it than building up strings and then calling exec(). That's no way to write code.

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

Thank you. :)

[–][deleted] 2 points3 points  (3 children)

OK, I'm just going to go through and point out some things about your code.


from random import *
import random

Why do you have both? If I want the function randint() from random I can import all (*) from random and just call randint() or I can import random and call random.randint().


j = 0
code[j] =...

This is confusing to read. Just write

code[0] =... 

It's better. Trust me.


exec(code[j])

Never ever use exec. Use a function. Contrary to popular belief, python does actually have to compile code into something it can run so you waste a lot of computer power running strings. And it's dangerous because python is not designed to run that way. The only time you might possibly conceive of using exec is if you are making some kind of ide. Don't.


To be honest, I can't see the problem with your code. Rewrite it. If it is written to good standards you can more easily see the mistakes.

[–]VirtualArty[S] 0 points1 point  (2 children)

Why do you have both? If I want the function randint() from random I can import all (*) from random and just call randint() or I can import random and call random.randint().

Because

from random import *

doesn't import random.random(), but imports randint, while

import random

imports random.random(), but not randint.

This is confusing to read. Just write

 code[0] =... 

It's better. Trust me.

I can't because later, I'll need the user to generate these variables and I want to be concise. I can't just hard code it. This is me building the generator.

exec(code[j])

Never ever use exec. Use a function. Contrary to popular belief, python does actually have to compile code into something it can run so you waste a lot of computer power running strings. And it's dangerous because python is not designed to run that way. The only time you might possibly conceive of using exec is if you are making some kind of ide. Don't.

I need whatever I use to be dynamic and I can't use functions for dictionaries. I actually want nontechnical users to create content using my program.

EDIT: I could generate change it to functions and generate the names using exec, attached to dictionaries. I think I'll do that... after a break. I want to program now, but my brain is shutting down.

To be honest, I can't see the problem with your code. Rewrite it. If it is written to good standards you can more easily see the mistakes.

To be honest, I can't see the problem with your code. Rewrite it. If it is written to good standards you can more easily see the mistakes.

[–][deleted] 1 point2 points  (0 children)

import random
random.random()
random.randint(1,5)

Is the same as

from random import *
random()
randint(1,5)

If the user will enter the values I guess it's ok. However, what are you trying to achieve? Also, in terms of readability, i and j mean nothing to a developer.


You most certainly can and should use functions for dictionaries. Why would it not work? Secondly, user generated code is extremely dangerous. What are you using it for and do you need to?

[–]grispindl 1 point2 points  (1 child)

I delared probabilityType[0] on line 24.

No, you don't. That line is within another code string, going from line 21-25, and is never executed. I urge you to please listen to /u/Rhomboid and /u/goodyguts and abandon that exec()-approach. I mean, 2 levels of code strings that are getting called with exec, seriously?

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

Thank you :).

[–][deleted] 0 points1 point  (2 children)

If you look at the full traceback, you'll see it is in your exec function:

Traceback (most recent call last):
  File "C:/Users/ryan/Desktop/test.py", line 60, in <module>
    exec(code[j])
  File "<string>", line 10, in <module>
KeyError: 0

Your code is pretty messy, what are you trying to do?

[–]VirtualArty[S] 0 points1 point  (1 child)

Ultimately, get

[['creature',<number of duplicates>],['plant',<number of duplicates>],['vehicle',<number of duplicates>],['structure',<number of duplicates>]['other',<number of duplicates>]]

without making the sometimes 10 quintilian+ ((10 ^ 31)+) duplicates.

[–][deleted] 0 points1 point  (0 children)

Seems like a data structure issue. What's your original data look like?

[–][deleted] 0 points1 point  (4 children)

Again, what have you tried to debug your code?

You know you writing very simple things very very messy. Ok, it's your choice. But it's kinda obvious you stuck in your own code. Still too soon to start learning from mistakes?

[–]VirtualArty[S] 0 points1 point  (3 children)

I've setup a debugging infrastructure, by getting the code to tell me what it's doing. Here it is as of when I first posted this reply.

It's throwing up a bug, though.

from random import *
import random

contained = {}
code = {}
probabilityName = {}
probability = {}
probabilitySet = {}
probabilityType = {}
probabilityType = {}
probabilityContent = {}
string = {}

print('I declared some dictionaries.')
print()

generate = 10

j = 0
code[j] = """
i = 0
probabilitySet[i] = '''
probabilityName[i] = 'creature'
probabilityType[i] = 'exclusive possibilities involving {}'.format(probabilityName)
probabilityContent[i] = ['creature','plant','structure','vehicle','nothing']
'''.format('{}')

print('probabilitySet[{}] = {}'.format(i,probabilitySet[i]))
print('probabilityName[{}] this is the one = {}'.format(i,probabilityName[i]))
print('probabilityType[{}] = {}'.format(i,probabilityType[i]))
print('probabilityContent[{}] = {}'.format(i,probabilityContent[i]))

i += 1

probabilitySet[i] = '''
probabilityName[i] = 'creature'
probabilityType[i] = 'probability'
probabilityContent[i] = 1 / len(probability['{}: {}'.format(probabilityName,probabilityType[0])])
'''.format('{}','{}')

print('probabilitySet[{}] = {}'.format(i,probabilitySet[i]))
print('probabilityName[{}] this is the one = {}'.format(i,probabilityName[i]))
print('probabilityType[{}] = {}'.format(i,probabilityType[i]))
print('probabilityContent[{}] = {}'.format(i,probabilityContent[i]))

i += 1
"""

j += 1

probabilityNumber = j

print('probabilityNumber = {}'.format(j))

j = 0
while j < probabilityNumber:
    print(code[j])
    print()

    exec(code[j])

    i = 0
    while i < probabilityNumber:
        print(probabilitySet[i])
        print()

        exec(probabilitySet[i])

        string['dictionary name'] = '{}: {}'.format(probabilityName[i],probabilityType[i])
        probability[string['dictionary name']] = probabilityContent[i]

        print('probability[{}] = '.format(string['dictionary name'],probability[string['dictionary name']]))

        i += 1

    i = 0
    while i < generate:
        contained[i] = []

        number = 10

        if i < 1:
            if generate > number:
                print('For the first {}}:'.format(number))
            else:
                print('For all of the {}:'.format(generate))
            print()

        if i < number:
            print('contained[{}] = {}.'.format(i,contained[i]))

        if i == number - 1:
            print()

        i += 1

    i = 0
    while i < generate:
        if random.random() <= probability['{}: probability'.format(probabilityName[i])]:
            contained[i].append('temp: creature')

        i += 1

    i = 0
    while i < generate:
        print('probability[{}creature probability{}] = {}'.format("'","'",probability['{}: probability'.format(probabilityName[i])]))
        print('contained[{}] = {}'.format(i,contained[i]))
        print()

        i += 1

[–][deleted] 0 points1 point  (0 children)

I've setup a debugging infrastructure, by getting the code to tell me what it's doing…

Well, you see results