Python Forum
Iterate randint() multiple times when calling a function
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Iterate randint() multiple times when calling a function
#1
Sad 
When I call the stars() function, it only iterates the randint() function once and not every time the while loop is ran. I am unsure on how to make it call a different randint() each time the while loop runs. Any help would be greatly appreciated.

from turtle import Turtle, bgcolor, colormode, done
from random import randint
bgcolor("blue4")
colormode()


def main() -> None:
"""The entrypoint of my scene."""
van_gogh: Turtle = Turtle()
stars(van_gogh, randint(-400, 400), randint(0, 250), randint(10, 30))
done()


def stars(a_turtle: Turtle, x: int, y: int, radius: int) -> None:
"""Describing the stars' characteristics, location, and drawing orders."""
a_turtle.fillcolor("yellow")
a_turtle.penup()
a_turtle.goto(x, y)
a_turtle.pendown()
i: int = 0
while i < 20:
a_turtle.begin_fill()
a_turtle.circle(radius)
a_turtle.end_fill()
i = i + 1


if __name__ == "__main__":
main()
Yoriz write Feb-16-2022, 12:30 AM:
Please post all code, output and errors (in their entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.
Reply
#2
stars() takes x,y as ints to describe the location. That's not going to change within stars.

If you want one call to stars() to draw random locations, make that explicit and don't pass in the location. Pass in a range and let stars pick randomly in that range.

Otherwise, just make stars() draw one star at one location and then call it as many times as you need. Something like:


# draw_star() draws one star at a time.
for _ in range(20):
    draw_star(van_gogh, randint(-400, 400), randint(0, 250), randint(10, 30))
Reply
#3
If you want a different random number each time the loop runs, call randint() from inside the loop. Seems pretty obvious. Am I missing something?

Let me take that back. Instead I think you should take the loops outside the stars function and put it in the main function. stars() becomes star() and it draws one star. Call multiple times to draw multiple stars.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  random numbers, randint janeik 2 574 Nov-27-2023, 05:17 PM
Last Post: janeik
  Unexpected output while using random.randint with def terickson2367 1 517 Oct-24-2023, 05:56 AM
Last Post: buran
  calling external function with arguments Wimpy_Wellington 7 1,451 Jul-05-2023, 06:33 PM
Last Post: deanhystad
  Calling a function (which accesses a library) from another file mouse9095 4 832 Jun-07-2023, 08:55 PM
Last Post: deanhystad
  Calling a class from a function jc4d 5 1,831 Dec-17-2021, 09:04 PM
Last Post: ndc85430
  [Solved] TypeError when calling function Laplace12 2 2,898 Jun-16-2021, 02:46 PM
Last Post: Laplace12
  Function - Return multiple values tester_V 10 4,468 Jun-02-2021, 05:34 AM
Last Post: tester_V
  Using Dictionary to Test Evenness of Distribution Generated by Randint Function new_coder_231013 6 3,298 Feb-23-2021, 01:29 PM
Last Post: new_coder_231013
  calling a function and argument in an input phillup7 3 2,626 Oct-25-2020, 02:12 PM
Last Post: jefsummers
  Help with a random.randint choice in Python booponion 5 2,814 Oct-23-2020, 05:13 PM
Last Post: deanhystad

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020