1

I'm having almost the exact same issue as here, except the solution listed there did not work for me.

I'm running this on my local machine, and my memory and CPU usage seems fine in task manager. (6 cores, 12 gigs of ram)

I saw this problem first in with an online IDE called repl.it. I have a simple code that generates a Math problem in the form of an SQL entry.

Here is a screenshot of my output.

The loop was only ran 5 times as opposed to the requested 300, then froze. I ran it again and the loop ran 7 times.

I repeated it, and each time, the number of times the loop ran varied drastically.

  • After making sure my code was error free, I thought perhaps there was something wrong with the online IDE, so I ran it on my local command console, however, the same thing ended up happening. The code would not run completely.
  • At this point, I decided there was something wrong with the print function, perhaps. I tried writing to a .txt file instead, and the code didn't run at all.
  • I realized there must be something wrong with my computer then, and
    ran the code on my old laptop. The same, exact thing happened.

Here is my full code:

import random

entries = input("Enter the number of entries you would like to insert");

sqltext = open("sql.txt", "w")
sqltext.write("INSERT INTO `toredatabase`.`questions` (`id`, `category`, `question`, `Correct_Answer`, `answer_A`, `answer_B`, `answer_C`, `answer_D`, `Details`) VALUES")
print("INSERT INTO `toredatabase`.`questions` (`id`, `category`, `question`, `Correct_Answer`, `answer_A`, `answer_B`, `answer_C`, `answer_D`, `Details`) VALUES")
for x in range(int(entries)):

  r1 = random.randint(0, 1000)

  r2 = random.randint(0, 1000)

  correctanswer = r1+r2
  possibleanswers = []
  ans1 = r1+r2+random.randint(-100,100)
  ans2 = r1+r2+random.randint(-100,100)
  ans3 = r1+r2+random.randint(-100,100)
  ans4 = r1+r2+random.randint(-100,100)
  possibleanswers.append(correctanswer);
  possibleanswers.append(ans1);
  possibleanswers.append(ans2);
  possibleanswers.append(ans3);
  a = random.choice(possibleanswers)
  b = random.choice(possibleanswers)
  while b == a:
    b = random.choice(possibleanswers)
  c = random.choice(possibleanswers)
  while c == a or c == b:
    c = random.choice(possibleanswers)
  d = random.choice(possibleanswers)
  while d == c or d == b or d == a:
    d = random.choice(possibleanswers)
  print(f"('', 'Math', 'What is {r1} + {r2}?', '{correctanswer}','{a}', '{b}', '{c}', '{d}', 'Addition'),")  
  sqltext.write(f"('', 'Math', 'What is {r1} + {r2}?', '{correctanswer}','{a}', '{b}', '{c}', '{d}', 'Addition'),")

You can run it yourself and see if you are met with similar results.

What in the world is the problem here?

Bsquare ℬℬ
  • 4,423
  • 11
  • 24
  • 44
  • Welcome to StackOverflow! Read about [how to ask a question](https://stackoverflow.com/help/how-to-ask) (particularly [how to create a good example](https://stackoverflow.com/help/mcve)) in order to get responses. You can also [take the tour](https://stackoverflow.com/tour). – Alex Mar 03 '18 at 23:40
  • One of those while loops might be going on forever. – whackamadoodle3000 Mar 03 '18 at 23:42
  • You're right. I didn't take into to make random answer generation non duplicate. That is likely the problem. – Brandon Liu Mar 04 '18 at 00:24

1 Answers1

0

To be sure, you should stop the running script with ctrl+c or some other way which displays the stacktrace on exit. This will show you which code you're executing at the time.

But the likely reason is that your loops never finish, because you randomly chose the same number twice for an answer (you're selecting 4 numbers out of 200, doing it 300 times - it's quite likely), so:

  while d == c or d == b or d == a:
    d = random.choice(possibleanswers)

Will always end up with a duplicate.

Instead of doing random choosing of answers, you can shuffle:

random.shuffle(possibleanswers)
a, b, c, d = possibleanswers

To generate the new answers without duplicates, you could do something like:

correctanswer = r1+r2
domain = set(correctanswer+x for x in range(-100, 100))
domain.discard(correctanswer)  # make sure you don't select the correct one
possibleanswers = random.sample(domain, 3)  # select 3 random ones
possibleanswers.append(correctanswer)
viraptor
  • 33,322
  • 10
  • 107
  • 191