#πŸ”’ Extra Boxes in Rows

31 messages Β· Page 1 of 1 (latest)

north grail
#

I made a small program that generates boxes based on a random number under 7. The generation works fine, but sometimes, the boxes exceed the row limit i made. could i please get some help?

cosmic haloBOT
#

@north grail

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

fervent drift
#

!paste

cosmic haloBOT
#
Pasting large amounts of code

If your code is too long to fit in a codeblock in Discord, you can paste your code here:
https://paste.pythondiscord.com/

After pasting your code, save it by clicking the Paste! button in the bottom left, or by pressing CTRL + S. After doing that, you will be navigated to the new paste's page. Copy the URL and post it here so others can see it.

fervent drift
#

please put your code in here

north grail
#

alr

#
#                                     Random Coloured Pixels

from random import *
from turtle import *


bgcolor("Black")
pensize(10)
hideturtle()
speed(0)
penup()
goto(-350, 340)
pendown()
repeat = int(input("How many Rolls? (WARNING: deformation of rows may occur after 100+ rolls): "))
pencolor("white")
red = 0
orange = 0
yellow = 0
green = 0
blue = 0
purple = 0


for count in range(repeat):
  rolled = randint(1,6)
    
  if rolled == 1:
    pencolor("Red")
    red += 1
    for count in range(4):
      forward(10)
      right(90)
    penup()
    forward(25)
    pendown()
             
  if rolled == 2:
    orange += 1
    pencolor("Tomato")
    for count in range(4):
      forward(10)
      right(90)
    penup()
    forward(25)
    pendown()

  if rolled == 3:
    yellow += 1
    pencolor("Yellow")
    for count in range(4):
      forward(10)
      right(90)
    penup()
    forward(25)
    pendown()

  if rolled == 4:
    green += 1
    pencolor("Green")
    for count in range(4):
      forward(10)
      right(90)
    penup()
    forward(25)
    pendown()
    
  if rolled == 5:
    blue += 1
    pencolor("Blue")
    for count in range(4):
      forward(10)
      right(90)
    penup()
    forward(25)
    pendown()
    
  if rolled == 6:
    purple += 1
    pencolor("Purple")
    for count in range(4):
      forward(10)
      right(90)
    penup()
    forward(25)
    pendown()
          
  if xcor() >= 350:
    penup()
    setx(-350)
    sety(ycor() - 25)
    pendown()
    
print(f"""
    Frequencies
    
Red/1: {red}
Orange/2: {orange}
Yellow/3: {yellow}
Green/4: {green}
Blue/5: {blue}
Purple/6: {purple}""")

#

@fervent drift

#

this good?

fervent drift
#

yep! taking a look now

north grail
#

ty

fervent drift
#

onw thing I see immediately is that you have a lot of repeat code that could be simplified, but that can wait for a moment

north grail
#

yea

#

i can make it just change pen colors in the if statements, and then have the square making on the bottom

fervent drift
#

yep!

north grail
#

theres an extra box

fervent drift
#

mhmm, I see what you mean

north grail
#

how do i fix that?

fervent drift
#

How indeed, interestingly it seems to happen at consistent intervals. Let's take a look at what xcor is in each loop. I'll add a print right before your final if statement there, you do the same and let's see if we can't find a pattern

north grail
#

ale

#

r

#

wierd, theres decimals

fervent drift
#

yep, that's what I see, too. As I suspected, floating point errors. I'm guessing that the turtle module is doing some weird math to adjust the size of the "distance" units to the size of the viewport, though I can't see anything about it in the docs. For now, I'd say check for xcor() > 348 or something like that and leave a comment for why it's not quite 350

north grail
#

alr tysm

fervent drift
#

good luck! This is a fun little program, come back and post if you do more with it!

north grail
#

Sure!

#

IT WORKED

#

TYSM

fervent drift
#

πŸŽ‰ πŸŽ‰ πŸŽ‰ πŸŽ‰

cosmic haloBOT
#
Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.