#πŸ”’ Loop error?

111 messages Β· Page 1 of 1 (latest)

calm tundra
#

Hello! May I please get help on this? I run my code yet it says rat is not defined even though I defined the rat before calling it. Is there any way I can fix this?

timid compassBOT
#

@calm tundra

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.

calm tundra
#

def rat(x,y):
rect1 = Rectangle(60,30)
rect1.set_position(x,y)
rect1.set_color(Color.gray)
add(rect1)
rect2 = Rectangle(50,30)
rect2.set_position(x + 35,y - 20)
rect2.set_color(Color.gray)
add(rect2)
rect3 = Rectangle(20,20)
rect3.set_position(x + 20,y - 30)
rect3.set_color(Color.gray)
add(rect3)
rect4 = Rectangle(10,10)
rect4.set_position(x + 60,y - 15)
rect4.set_color(Color.black)
add(rect4)
rect5 = Rectangle(12,12)
rect5.set_position(x + 27,y - 23)
rect5.set_color(Color.red)
add(rect5)
rect6 = Rectangle(12,5)
rect6.set_position(x + 81,y -5)
rect6.set_color(Color.black)
add(rect6)
rect7 = Rectangle(52,5)
rect7.set_position(x - 50, y + 20)
rect7.set_color(Color.black)
add(rect7)
rat()

x_position = 120
y_position = 350

for _ in range (5):
rat(x_position, y_position)
x_position +=20

#

The error message was that rat was not defined

#

I was expecting this code to make the rat to move across the screen

#

I figured it out but now I have another problem

#

The rat

#

doesnt move

#

at all

#

I think it might have to do with the loop

#

im not sure

hollow cliff
#

!paste send your entire program in this pastebin

timid compassBOT
#
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.

hollow cliff
#

all 188 lines

calm tundra
#

okay

#

okay

#

I just did

charred cradle
#

rat is defind within draw_scene1, so it doesn't exist outside of it

#

but then you try to call it outside of it

calm tundra
#

hmmm

#

I get its cause it doesnt exist within the move across function but I honestly dont know what you mean

#

Im kinda wondering how to fix that

charred cradle
#
def a():
  b = 2

print(b) # error, b does not exist here```
calm tundra
#

yea

#

i understand that part

#

wait

#

let me try doing something

#

I will come back

#

yeah

#

nvm

#

I couldnt fix it

#

I thought putting the loop within the function would solve it but no

charred cradle
#

You can define the rat function outside of the loop

calm tundra
calm tundra
#

could u give me an example

#

without using my work

#

please

#

and thank u

charred cradle
#
def a():
  print(1)

def b():
  a()

a()```
calm tundra
#

OHHH

#

ok

#

honestly

#

I have absolutely no clue what I'm doing wrong

#

it didnt worl

#

work***

hoary ivy
#

What's the exact error you're getting?

#

Oh wait I see

calm tundra
#

Do you possibly know how I can overcome it?

#

;-;

charred cradle
#

What is the code like after changing it?

calm tundra
#

hold on

#

want a screen shot or should i copy paste

#

def rat(x,y):
rect1 = Rectangle(60,30)
rect1.set_position(x,y)
rect1.set_color(Color.gray)
add(rect1)
rect2 = Rectangle(50,30)
rect2.set_position(x + 35,y - 20)
rect2.set_color(Color.gray)
add(rect2)
rect3 = Rectangle(20,20)
rect3.set_position(x + 20,y - 30)
rect3.set_color(Color.gray)
add(rect3)
rect4 = Rectangle(10,10)
rect4.set_position(x + 60,y - 15)
rect4.set_color(Color.black)
add(rect4)
rect5 = Rectangle(12,12)
rect5.set_position(x + 27,y - 23)
rect5.set_color(Color.red)
add(rect5)
rect6 = Rectangle(12,5)
rect6.set_position(x + 81,y -5)
rect6.set_color(Color.black)
add(rect6)
rect7 = Rectangle(52,5)
rect7.set_position(x - 50, y + 20)
rect7.set_color(Color.black)
add(rect7)

def move():
    x_position = 120
    y_position = 350

    for x in range (5):
        rat(x_position, y_position)
        x_position += 20
        
rat()
timid compassBOT
#

Hey @calm tundra!

Please edit your message to use a code block

```py
print('Hello, world!')
```

This will result in the following:

print('Hello, world!')```
charred cradle
#

does this still give the same error? It doesn't seem like it should

calm tundra
#

it runs

#

just not how I want it to

#

wops

#

woops

#

I shouldve mentioned that

#

sory

#

sorry

#

so

#

Basically

hoary ivy
#

What do you want it to do and what does it do?

calm tundra
#

I want it to move to the right

#

the mouse

#

or rat

#

I created a loop so that

#

It would just add on 20 units

#

and move it to the right

#

but

#

when I press run

#

it just disappears

#

by disappear I mean the rat disappears not the whole code

hoary ivy
#

I'll try running it in a second

calm tundra
#

okay

#

ty

hoary ivy
#

can you send over the entire file?

calm tundra
#

sure

#

hold on

hoary ivy
#

is there anything you've imported or defined in a different file? Rectangle is never defined in this file for example

calm tundra
#

Ohhhh

#

no

#

Im pretty sure that comes within the program

hoary ivy
#

right ok.. looks like ima have to find whatever you're using then 😭

calm tundra
#

sorry

#

I DID IT

#

I DID IT

#

DUDE

#

but now I have another problem ;-;

#

I have quadruple amount of rats..

hoary ivy
calm tundra
#

yes

#

smh

#

it was so simple

hoary ivy
#

that's what I thought the problem might be - rat() was defined outside that scope, so it didn't think it was defined

calm tundra
#

yea

hoary ivy
# calm tundra

Ig you'll need to make a function to delete the old rat
The current one just makes a new rat

calm tundra
#

😭

#

i have 1 gun 3 bullets

hoary ivy
#

rip

calm tundra
#

:(

timid compassBOT
#
Python help channel closed for inactivity

This help channel has been closed. 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.