#🔒 Problem using a variable inside a function as a conditional for a while loop

115 messages · Page 1 of 1 (latest)

bitter swift
#

Hi. Im trying to run a function that makes some changes to a matrix, the problem here is that i need it to run on a while loop while a var called "burningtrees" is > 0. ive tried returning the variable but there is a problem bc i get an "int type is non suscriptable". Code: https://paste.pythondiscord.com/4ZAA

tender geodeBOT
#

@bitter swift

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.

crimson pendant
#

What exactly is the full error? These lines look sketchy though:

incendio(bosque)
bosque=incendio(bosque)

You call the same function twice in a row with the same argument, and overwrite bosque only the second time?

#

That wouldn't cause that error alone, but it looks off.

bitter swift
#

hm

#

thats right

#

that was causing for the matrix to print twice in each step

crimson pendant
#

What's the full error again though?

#

Since that's a separate issue.

bitter swift
#

burningtrees= incendio(bosque)
burningtrees=burningtrees[1]
while burningtrees>0:
#preguntarle al santi como era por que lo borre y me re olvide

bosque=incendio(bosque)
tiempo+=1
tender geodeBOT
#

Hey @bitter swift!

It looks like you're trying to paste code into this channel.

Discord has support for Markdown, which allows you to post code with full syntax highlighting. Please use these whenever you paste code, as this helps improve the legibility and makes it easier for us to help you.

To do this, use the following method:
```py
print('Hello, world!')
```

This will result in the following:

print('Hello, world!')```
You can **edit your original message** to correct your code block.
bitter swift
#

rboles quemados: 0
Traceback (most recent call last):
File "c:\Users\Luciano\Desktop\Udesa\Pensamiento Computacional\Práctica\TP1\TP GOTY Edition.py", line 162, in <module>
bosque=incendio(bosque)
^^^^^^^^^^^^^^^^
File "c:\Users\Luciano\Desktop\Udesa\Pensamiento Computacional\Práctica\TP1\TP GOTY Edition.py", line 98, in incendio
vecino1=matriz2[columna-1][fila-1]
~~~~~~~~~~~~~~~~~~^^^^^^^^
TypeError: 'int' object is not subscriptable
PS C:\Users\Luciano\Desktop\Udesa\Pensamiento Computacional\Práctica>

#

there is it

#
    return matriz3, burningtrees

burningtrees= incendio(bosque)
burningtrees=burningtrees[1]
while burningtrees>0:
    #preguntarle al santi como era por que lo borre y me re olvide

    bosque=incendio(bosque)
    tiempo+=1```
#

there, when i made these last changes, i got the error

#

the first version of the code is the one that "works"

crimson pendant
#

Show the full code that causes the error. I thought the code you showed was the broken code.

bitter swift
#

sorry

#

let me show you

crimson pendant
#

Here you've got me doubting my debugging ability since the code you posted can not cause that error as far as I could tell.

bitter swift
#

there is a problem with te vscode debugger

#

when it gets to the list comprehension

#

just skip to the next breakpoint

#

i discovered it with my teacher

crimson pendant
#

return matriz3, burningtrees
bosque=incendio(bosque)

You're returning a tuple from the function, assigning that tuple to bosque, then treating that tuple as a matrix and passing the tuple as a matrix back in to incedio.

bitter swift
#

OHHHHHHHHH

#

i think i get it

#

i have to do

#

bosque=incendio(bosque[0])

#

something like this?

crimson pendant
#

burningtrees in an int, so with vecino1=matriz2[columna-1][fila-1], the second subscript is attempting to subscript burningtrees.

crimson pendant
bitter swift
#

hm

#

lemme try

#

IT WORKS

#

sorta

#

but its better than before

crimson pendant
#

New errors/problems are typically at least progress.

bitter swift
#

yes

#

look

#

i think im getting there

#

the problem here is that it runs indefinitely

#

xd

crimson pendant
#

Your loop is depedendant on burningtrees to stop (given the condition), but you never change burningtrees in the loop.

#

So if the loop is ever entered, it will run forever.

bitter swift
#

let me see how i can solve it

#

when it gets to the top line, wont it call the function again?

#

burningtrees=incendio(bosque)

#

sorry for the crop

crimson pendant
#

incwendio returns a tuple still doesn't it? So bosque contains the burningtrees value, but you never actually reassign burningtrees there.

bitter swift
#

oooo

#

okokokok

#

smth like that

crimson pendant
#

Ya, or using the unpacking that I showed. Both work.

#

Wait no, that won't work.

bitter swift
#

im getting another error

pliant hawk
#

mixing languages in variable names :)

bitter swift
#

hi

#

wym?

crimson pendant
#

[0] is removing burningtrees before assigning it to bosque.

bitter swift
#

now i see

#

thats right

pliant hawk
bitter swift
#

IT WORKS}

#

I THINK)

#

take a look

bitter swift
pliant hawk
bitter swift
#

oh yes

#

thats bc i ran oout of variable names

#

and "arbolesquemando" was not really an atractive name

pliant hawk
#

now you know why ppl usually write code in English :s

bitter swift
#

burnigtrees for the epic factor

bitter swift
#

but this is for an assignment

pliant hawk
#

so at least burning_trees

bitter swift
#

im on my first year of engineering

pliant hawk
#

GL

bitter swift
#

thanks bro

bitter swift
#

thanks @pliant hawk @crimson pendant

pliant hawk
#

did you write the comments in the code?

bitter swift
#

yes

#

xd

#

its all made by me

pliant hawk
#

I would not format them as they are

bitter swift
#

spaghetti at its finest

pliant hawk
#

makes it hard to read

bitter swift
#

hm

pliant hawk
#

just put one space after the #

bitter swift
#

ok ok

#

i noticed vsc tends to do that sometimes

pliant hawk
#

also use them sparingly

bitter swift
#

thx bro

bitter swift
pliant hawk
#

don't overuse them

bitter swift
#

oh

#

yes

#

they are for me in future

pliant hawk
#

such as "PASO 0"

#

this means "step 0" right?

bitter swift
#

yes

pliant hawk
#

it's not really a helpful comment IMO

bitter swift
#

youre right

pliant hawk
#

I can give some more "tips" if you want?

#

e.g

# FUEGO CENTRAL
centro = dimension // 2

for d_fila in (-1, 0, 1):
    for d_columna in (-1, 0, 1):
        bosque_vacio[centro + d_fila][centro + d_columna] = n
#

oh also sometimes you write this matriz2[columna][fila], which isn't actually correct

#

the "fila" should be first

tender geodeBOT
#
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.