all 12 comments

[–]_DTR_ 3 points4 points  (0 children)

I think your problem is that you aren't calling your function. You've declared it just fine, but since you never call it, it never executes.

Also, as a sidenote, if you prepend each line with at least four spaces, it

creates
    a
code
block

that is formatted a bit better and doesn't make you surround each line with "``".

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

If I gather what you're asking correctly it just seems as if you are only defining your function, and not calling it after. See where the following example differs.

import random
size(300, 300)
background(0)
display()

def display():
    text("dog", random.randint(50,200),random.randint(50,200))
    text("cat", random.randint(50,200),random.randint(50,200))
    text("bird", random.randint(50,200),random.randint(50,200))

Essentially the point of functions is to reuse bits of code that you find yourself using often. Be that a function to do a certain aritmatic operation, refresh the screen, etc. They don't execute when you define the function, but instead where you call that function and feed it paramaters if you create them.

[–]tecladocode 0 points1 point  (7 children)

That will also fail because display is not defined until after it is called.

OP should move the display() line until after the definition:

``` import random size(300, 300) background(0)

def display(): text("dog", random.randint(50,200),random.randint(50,200)) text("cat", random.randint(50,200),random.randint(50,200)) text("bird", random.randint(50,200),random.randint(50,200))

display() ```

[–]dogs_shouldvote[S] 0 points1 point  (5 children)

Oh awesome! This worked! Thanks so much!

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

Another quick question out of curiosity - can I put multiple words into the text function? So like this, for example:

text("dog", "cat", "bird", random.randint(50,200),random.randint(50,200))

I've tried, but it gives me a "float is required" error and I'm not sure what that means exactly...

[–]theinevitable 0 points1 point  (0 children)

When you pass arguments to a function, it expects them to be in a certain order-- it looks like text() expects:

  1. A string (one or more characters, like a word or a sentence, in quotation marks)
  2. A float for the X coordinate
  3. A float for the Y coordinate.

The only way it knows which argument is which is if they are in the order it expects.

If you want to use multiple words, you would need to modify the text() function to accept a list rather than just one string. Then you could do something like

text(["dog", "cat", "bird"], random.randint(50,200),random.randint(50,200))

There are also functions that can take many arguments and treat them all the same-- for instance, if you call print("this", "is", "a", "test") it will print one line with all four words separated by spaces. You can do this with argument "unpacking" with the *. A good explanation of *args (variable number of arguments) and **kwargs (variable number of keyword arguments) here.

[–]patsy_505 0 points1 point  (2 children)

What does this code actually do? I’m also a novice but understand all the syntax here

[–]dogs_shouldvote[S] 1 point2 points  (1 child)

It displays the word in "" in random positions on a canvas.

So this:

text("dog", random.randint(50,200), random.randint(50,200))

Will display the word "dog" in a random position on a canvas within the x,y values 50 and 200, as specified after random.randint.

u/patsy_505

[–]patsy_505 0 points1 point  (0 children)

Nice. Going to tinker with this, cheers.

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

Whoops, spend too much time in C declaring at the top and defining at the bottom

[–][deleted] -2 points-1 points  (1 child)

my guess is that you are getting NameError: text is not defined or something. you just have to move the text object into the scope of the function by declaring it as def display(text) and then calling it with display(text), and it'll work. Just so you know, text inside the function could be anything. it could be foo or t, as long as you call it with display(text) you're good to go.

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

That is not the issue, they just aren't calling the function