Quiz variables
Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller
Quiz variables
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
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
-
- Livecode Opensource Backer
- Posts: 9388
- Joined: Fri Feb 19, 2010 10:17 am
- Location: Bulgaria
Re: Quiz variables
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.
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.
-
- VIP Livecode Opensource Backer
- Posts: 9665
- Joined: Wed May 06, 2009 2:28 pm
- Location: New York, NY
Re: Quiz variables
Hi.
Welcome.
Two things. First, please put code between the code tags "</>" like this:
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
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
...
Craig
-
- Posts: 101
- Joined: Mon Jan 03, 2022 7:10 pm
Re: Quiz variables
I built a stack based on your code and it worked, so not too certain what's different with yours.
Re: Quiz variables
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
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:
Or some such anyway
HTH
Stam
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
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
HTH
Stam
-
- VIP Livecode Opensource Backer
- Posts: 4002
- Joined: Sun Jan 07, 2007 9:12 pm
- Location: Bochum, Germany
Re: Quiz variables
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
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
-
- VIP Livecode Opensource Backer
- Posts: 9665
- Joined: Wed May 06, 2009 2:28 pm
- Location: New York, NY
Re: Quiz variables
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
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