i looked again at your code and made it run offline and it works.
So not sure here. For a second I thought you forgot to eval the file on read but I was wrong.
Do you get some exception error? I hope
This is not repl fault.
i looked again at your code and made it run offline and it works.
So not sure here. For a second I thought you forgot to eval the file on read but I was wrong.
Do you get some exception error? I hope
This is not repl fault.
itâs an issue of the scope of variables. i donât fully understand it yet, but subroutines canât access global variables. since youâre defining âinventoryâ in the main body, itâs a global variable. if youâre using the replit editor, it should show you this yellow squiggly line:
this tells you that the subroutines couldnât access the global variable âinventoryâ, so it treats it as a local variable instead. this means any data read into this local variable will be unaccessible outside the subroutine unless itâs explicitly returned.
your code works if you load the global variable with âglobalâ (notice how the squiggly line disappeared):
however use of âglobalâ is considered a bad practice (apparently it can slow things down, among other things). so another way is to use a local variable inside the subroutine into which you load the file, then return that variable:
in the main loop assign that value to âinventoryâ
youâll need to return an empty list from the except part too:
as to why the other subroutines can seemingly interact with global variables, i guess they donât actually load it, but only send instructions what to do with it. in visual studio code you can see this when hovering over variables: even though âinventoryâ is defined as a list, the subroutine doesnât know this because it hasnât loaded it:
whereas when hovering over inventory in the mainbody it is correctly identified as a list:
i havenât properly studied the topic of scope yet, so i could be wrong on the details. but this issue has mystified me several timesâŚ
Inventory is global and this means you need to handle it as such in the procedures,
The best is to never use global variables and always pass them as arguments.
Thanks! This really helped me a lot!
But yeah, would be interesting to comprehend why the other subroutines had no problem accessing the global list.
It depends if they read or write and how. Just avoid globals and you will be fine for the rest of your programming life!
https://replit.com/@arhanansari2009/RPG-Inventory-System?v=1
I have solved Day 53
see my code
P.S:Already completed Day 100