Create variables as the code runs

Hi folks,

I have built a simple of Rummy game - see here:

It uses lists for each player (Hand1, Hand2 etc) but this is hard coded. so currently it always plays with 4 Hands. What I am trying to do is something like this:
-1- Show: "How many players?"
-2- For i from 1 to
-3- Set variable name to join "Hand"+i

in other words - create variables when the code runs. that way I can create only the number of variables I need for the number of players.

I have searched but can not find a solution? is this even possible?

File -> Libraries... -> create variables in program

Incredible... I had never spotted the libraries !! there is great stuff in there !

problem sorted:


Actually building it into the code it's not working - I'm having trouble setting the Hand1 variable created to a 'list variable' any advice ?

Ah, I regret that we put the variable library in for everybody, it was meant for curriculum developers, not for project programmers. @bh can we discuss this separately? This is exactly the kind of confusion that catering to the "hackers" in our forum begets, now everybody thinks they have to use meta-programming to solve simple tasks.

@chaintong: You just need one variable, and you can set it to be a list, and then you can store everything in that list.

using lists is much simpler and faster

@jens: It's interesting, I kinda agree with you, but back in the day we did this sort of dynamic variable creation all the time. See, for example, In Logo you can just assign a value to a previously-unbound variable name and it makes a global binding. I see it as the opposite of an advanced-for-hackers-only feature! It's for people who started in Fortran or something, and haven't yet become comfortable with content-addressable lists of lists. But sure, let's talk...

@chaintong: There's no such thing as a "list variable." Any variable can take on any value of any data type. So if you assign a list to a variable, then it acts like a list variable, but if you later assign a number to the same variable, it acts like a numeric variable.

There are always lots of ways to write the same program. Your way, making variables, is fine -- don't let Jens intimidate you! :~P Jens's way, putting all the hands into one big list of lists, is fine too. Yet a third way is object oriented: Make a "player" sprite, give it a this-sprite-only variable Hand, and clone the sprite as many times as you have players. Then your code just uses the name Hand, and whichever sprite is running, that's the hand that's used.

@bh :
We had realised (having done some reading) that we should have done exactly as you suggested: each hand should have been a sprite…. But object oriented coding is a bit harder to get our heads round. Version 3 !

We had thought about Jens solution when we were 'designing' it but the code requires a lot of sorting of hands, so one list would make it hard to keep track of which card belongs to which hand.

Back to V2 – setting the number of players and getting a human player in the game…

From what you say it sounds like it must be possible to do what I need, but I can’t see it…

What needs to happen is like the code below: deal 7 cards to each player but it throws an obvious error – I’m passing a string, not a variable into the “add to list fucntion”,

I always come back to one of two problems:
-1- either im passing in a string to the "add thing to list"
-2- or I’m passing in a variable that has not been “set to list “

I'm clearly missing something.

BTW I've opened up the librarly block Global variables, as well to see if I could fix it there.... but its a bit too 'advanced' in there!

Hi, @chaintong,

Yes, you can do all these things. I show you what you can do now... and leave aside (at the moment) discussions about "create variables in program" library, because we have other threads with these details and i will submit a proposal with the "agreed" changes (I have a pending conversation with Brian and Jens).

At the moment, doing


You have your new lists ready. And then, you can read them:


And do whatever you want


That's all. Snap! is great!!


I never remember that you can do those things! I was just reinventing VARIABLE and SET VARIABLE blocks...