Quiz variables

Got a LiveCode personal license? Are you a beginner, hobbyist or educator that's new to LiveCode? This forum is the place to go for help getting started. Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller

Post Reply
Kyler
Posts: 1
Joined: Mon May 15, 2023 6:06 pm

Quiz variables

Post by Kyler » Mon May 15, 2023 6:33 pm

Hi!
I am new to coding and livecode. I am working on a project for class and part of it is a quiz where depending on the answer they select it will recommend different movies. I tried to use global variables but it is not working how I want it. The last card under the quiz section, it should input the movie recommendation into the field based on their selections. Any help is appreciated!

This is the code of my result card :
global gGenreType, gRatingType, gRunTime

on openCard
put (gGenreType && gRatingType && gRunTime) into theResult
-- put theResult into msg
if (gGenreType is "Psychological") and (gRatingType is "Rotten Tomato") and (gRunTime is "More than 2hrs") then
put "Us 2019" into field "Movie Result"
end if
if (gGenreType is "Psychological") and (gRatingType is "Audience Score") and (gRunTime is "More than 2hrs") then
put "Shutter Island 2010" into field "Movie Result"
end if
if (gGenreType is "Psychological") and (gRatingType is "Rotten Tomato") and (gRunTime is "Less than 2hrs") then
put "Get out 2017" into field "Movie Result"
end if
if (gGenreType is "Psychological") and (gRatingType is "Audience Score") and (gRunTime is "Less than 2hrs") then
put "Us 2019" into field "Movie Result"
end if
end openCard

This is the code on my one of my buttons

global gRunTime

on mouseUp
put the short name of me into gRunTime
go to card "Movie Result"
end mouseUp

richmond62
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 9388
Joined: Fri Feb 19, 2010 10:17 am
Location: Bulgaria

Re: Quiz variables

Post by richmond62 » Mon May 15, 2023 8:29 pm

I cannot understand most of that, but one thing I do understand is that

1. You have a function in an openCard statement.

2. You supply data with a button on a different card that will disappear as soon as the mouseUp operation finishes.

You need to store your data so the openCard code can access it.

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9665
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

Re: Quiz variables

Post by dunbarx » Mon May 15, 2023 8:41 pm

Hi.

Welcome.

Two things. First, please put code between the code tags "</>" like this:

Code: Select all

Your handler calls out for the "switch" control structure:

switch
case (gGenreType is "Psychological") and (gRatingType is "Rotten Tomato")...
  put "Us 2019" into field "Movie Result"
break

case (gGenreType is "Psychological") and (gRatingType is "Audience Score")...
put "Shutter Island 2010" into field "Movie Result"
break

...
But what is your issue? It is not yet clear to me how the user interacts with the program, and therefore what is not quite working. Can you explain?

Craig

Emily-Elizabeth
Posts: 101
Joined: Mon Jan 03, 2022 7:10 pm

Re: Quiz variables

Post by Emily-Elizabeth » Tue May 16, 2023 4:10 am

I built a stack based on your code and it worked, so not too certain what's different with yours.

stam
Posts: 2683
Joined: Sun Jun 04, 2006 9:39 pm
Location: London, UK

Re: Quiz variables

Post by stam » Tue May 16, 2023 9:20 am

Everyone has different coding styles. My inclination would be to not hard code data into functions, and not put what should be a handler inside an openCard handler, for example putting it in the stack script as an individual handler and calling it from openCard gives more flexibility.

I would for example store data in an array, which itself could be a custom property of the stack or an external file - eg text, binary array file or SQLite file - and filter the array for the user choices.

The array could be something like

Code: Select all

Put “psychological” into tArray[1][“genreType”]
Put “rotten tomatoes” into tArray[1][“ratingType”]
Put “more than 2 hours” into tArray[1][runTime]
Put “Us 2019” into tArray[1][“movie result”]

Put “psychological” into tArray[2][“genreType”]
Put “audience score” into tArray[2][“ratingType”]
Put “more than 2 hours” into tArray[2][runTime]
Put “Shutter island 2010” into tArray[2][“movie result”]

Etc
Obviously you wouldn’t normally construct the array this way, you’d normally retrieve from storage either as an array or have a handler build the array from stored data.

This gives you flexibility to store the movie data without having to physically go back in and hard code new additions or changes into your openCard handler which means building new standalone apps.

I would then filter the array for the user choices:

Code: Select all

global gGenreType, gRatingType, gRunTime, gMovies 
# gMovies contains the movie array

function getUserChoice
   Local tArray
   Filter elements of gMovies were \
       each [genreType] = gGenreType and \
       each [ratingType] = gRatingType and \
       each [runTime] = gRunTime \
       into tArray
       return tArray[“movie result”]
end getUserChoice
Or some such anyway ;)
HTH
Stam

bn
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 4002
Joined: Sun Jan 07, 2007 9:12 pm
Location: Bochum, Germany

Re: Quiz variables

Post by bn » Tue May 16, 2023 9:30 am

Hi Kyler,

You should add a test to the openCard handler that all globals are filled appropriately otherwise your evaluation will fail.

Also make sure that in the refering buttons the globals are declared outside of a handler as you did in your mouseUp handler.
And check spelling of global variables.

Kind regards
Bernd

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9665
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

Re: Quiz variables

Post by dunbarx » Tue May 16, 2023 2:01 pm

Kyler.

Lots of good advice.

But how far along are you in using LiveCode? Some of the suggestions were more advanced, and I do not want you to go off track in learning how to program.

And do you still have an issue with what you already have running?

Craig

Post Reply

Return to “Getting Started with LiveCode - Complete Beginners”