0

The goal is having random objects spawn on 3 positions once . the code works and looks like this but with more if statements and 3 small stataments:

if mainloop != 3:
  if smallloop1 != 1:
     if randomnumber == x:
        object.goto(x, y)
        mainloop += 1
        smallloop += 1

the problem is that the if statement doesnt stop so multiple objects spawn
im also not getting any error messages
I tried changing the if statements to while loops which didnt change anything except having to stop them with the break command or using a list instead of a random number which made the thing more complicated and again didnt change anything
Thanks in advance
Edit: small reproducible example:

        if bigloop < 1:
          if mediumloop1 < 1:
            if random1 == 1 or 2 or 3:
                print("first loop")
                bigloop += 1
                mediumloop1 += 1
            if random1 == 4 or 5 or 6:
                print("first loop")
                bigloop += 1
                mediumloop1 += 1

The problem is that it prints "first loop" twice.

user69420
  • 3
  • 4
  • 1
    Please share a [mcve]. If this is Python, and python-turtle, please tag accordngly. Thanks. `mainloop` is a turtle function; you might want to pick a different var name, and this will clash if you're doing `from turtle import *` (hopefully you're doing `import turtle` though). – ggorlen Sep 25 '22 at 14:22
  • Added it, also I didnt name the mainloop "mainloop" and I use "import turtle" but whats the difference between "from module import *" and "import turtle"? Thanks in advance. – user69420 Sep 27 '22 at 15:48
  • `from module import *` unpacks all of the module properties into the namespace, so instead of saying `turtle.Turtle()`, `turtle.Screen()` or `turtle.mainloop()` you say `Turtle()`, `Screen()` and `mainloop()`. But without the prefix there's a high likelihood a name in your module will clash with a turtle module name. This still isn't a [mcve]--I just see a tiny snippet without any context that I can't run. I should be able to copy-paste a small script (30-40 lines max, say) into my editor, press "go" and see the exact same problem as you. I have no context for what any of these var names are. – ggorlen Sep 27 '22 at 15:50
  • `if random1 == 4 or 5 or 6:` is definitely wrong though, this is an "always true" statement. You probably mean `if random1 == 4 or random1 == 5 or random1 == 6:` but there's probably a much less awkward way to go about achieving whatever you're trying to do here. This is a common gotcha, see [this](https://stackoverflow.com/questions/15112125/how-to-test-multiple-variables-for-equality-against-a-single-value). – ggorlen Sep 27 '22 at 15:52
  • The content is basically whole thing is meant to be a the spawn system for random objects at 3 different locations and random1 is just a random number for these objects, all these variables values are 0. But the "or" part was the problem of the code. Managed to fix it. Thank you, learned a lot. – user69420 Sep 27 '22 at 17:45

0 Answers0