I am working on lesson 7 of event-driven programming and wonder why I cannot declare my variables outside an onEvent block. I am working to create a Madlib and have to get user input and then write it out with the user’s responses, but I have to do all that inside of the onEvent block instead of just creating a variable to save the user’s input, then use that input with onEvent command to write out the information.
Can you add a link to the project?
Event driven Programming Lesson 7-11
Above is a copy of the program in question. Thank you in advance for your feedback
Thanks for sharing the link to your project
I remixed your project, and declared each variable above the on Event block, and then removed the “var” from each variable’s assignment inside of the on Event. When I run the app, it seems to work, the input I type is shown in the message.
One issue I sometimes come across with my students that might be related to what wasn’t working for you is if they have var in both spots-- above the onEvent and inside of the onEvent for the same variable.
This creates troubles, as a variable should only be declared with “var” one time in the code. Once the declaration has been done once, the “baggie” has been created and you can return to it and store new values in it without typing var again.
Does this help get your project working?
Thank you you for your reponse and solution, that is very helpful.
Thanks @lindsay.davis for a great explanation.