ASimpleQuizforAlice3.2:AskUserFunctions.
ByLanaDyckunderthedirectionofProfessorSusanRodger
DukeUniversityJune2009,addedPart2July2011,
EditedforAlice3useandPart3,4addedbyAnhTrinhJuly2016
Introduction and Set-up
Thistutorialwilldemonstratehowtocreateasimplequizusingthethreedifferentkindsof“askuser”functions:askuserforanumber,askuserfortrueorfalseandaskuserforastring.
OpenanewAlicegrassworld.ClickonSetupScene.ChooseBrowseGalleryByGroup,and
clickAnimals.AddonePenguin,oneCow,andthree
Chickens intoyourworld.Arrangeandresizetheobjectsso
theyareeasytosee.ClickEditCodewhenfinished.
Let'sgetstartedbysettinguptheworld.
Createanewsceneprocedurethatwewilluselatertoholdtheinstructionsforthequiz.
Step 1: Create the Quiz Procedure
Letsstartbycreatingaquizprocedure
ClickontheScene tabandchooseAddSceneProcedure
Namethenewprocedure:quiz.
ClickbackonmyFirstMethod.
NowwewilladdthequiztoMyFirstMethod.
MakesureyouareinMyFirstMethod.
Clickonthis.penguin intheobjecttree.
Dragthesay procedureintotheprocedureeditorandtypein:Timeforaquiz.
Draginasecondsay procedureandtypein:Thatisallfolks!
Clickonthis intheobjecttreeanddragthequiz procedureinbetweenthetwopenguinsaycommands
Step 2: Set Up MyFirstMethod
Nowwewillstartwritingthequizprocedure.Tomakeourcodeeasiertoreadwewilladdincomments.Commentsmakeitpossibletoaddinnotesaboutthecodelike,whatitwilldo,orwhowrotethecode.
Firstclickonthequizproceduretab.
Toaddacommentdraganddropthedoubleslashbuttonintothemethod
Step 3: Comments
Typeinthecommenteditor:Question1
Clickonthis.penguin intheobjecttree,haveitsay:Howmanyanimalsintheworld?andsetthedurationto2seconds.
Step 4: Question #1
Weneedavariabletostorethenumberinformationwegetfromtheuser.
Draginthebuttontoopenvariableoptionbox.
NameitanswerNumber.SelectWholeNumberbesidevaluetype,initializeitto0andclickOK.
Clickontheworld intheobjecttree.Undertheworld'sdetails,functions find“askuserforanumber.”Clickanddragtheaskuserforanumber overthe1andselectother...Enterthestring:Enterthenumberofanimals:
Step 4: Question #1Continued...
Nowwewilldeterminewhathappensiftheanswergiveniscorrectorincorrect.
DraganddropanIf blockfromthebottomofthewindowandsetittotrue.
Step 4: Question #1Continued...
ChooseRelational(WholeNumber)inthetruedrop-downlist.
Select“???==???“->answerNumber...->CustomWholeNumbertoenterinthevalue5.5isthecorrectanswertothisquestion.
Forthisquestionwhenthe“If”statementistrue,theansweriscorrect.Sowewillputourresponsetothecorrectanswerfirst.
Clickonthis.penguin intheobjecttree.UndertheIf:Dragthesay methodandtype:CorrectDragtheturn methodandselect1
Whentheifstatementisfalse,theanswerisincorrect.
UndertheElse:Dragthesay methodandtype:Sorry,thatisnotcorrect.
Step 4: Question #1Continued...
Runyourworldtotaketheonequestionquiz.
Nowletsmakeaquestionthataskstheuserforatrueorfalseresponse.
Dropinacommentandtypein:Question2Clickonpenguin intheobjecttree,haveitsay:Aretheremorechickensthanpenguins?andsetthedurationto2seconds.
Step 5: Question #2
Weneedavariabletostorethebooleaninformationwegetfromtheuser.
Draginvariable buttonfromthebottomofthewindow.
NameitanswerBoolean.
SelectBooleanandClickOK.
Inthepenguin'sfunctions,draganddropthe“getBooleanFromUser”overthetrue.
Typein:Clickontrueandfalse.
Step 5: Question #2Continued...
NowwewilladdinourresponsestotheanswerinanotherIf/Elsestatement.
DraganddropanIf/ElsestatementandselectanswerBoolean.
TheIfquestionacceptsjustthevariablebecauseanswerBooleanisabooleantype.
Step 5: Question #2Continued...
Nowcompletethequestion#2byaddingintheproceduresasshownbelow:
Step 5: Question #2Continued...
Runyourworldtotakethetwoquestionquiz.
Forourfinalquestionwewillasktheusertoenterastring.
Dropinacommentandtypein:Question3
Clickonpenguin intheobjecttree,haveitsay:Whatbuildingarewein?andsetthedurationto2seconds.
Step 6: Question #3
Weneedavariabletostorethestringinformationwegetfromtheuser.
Draginthevariable button.
NameitanswerString.
SelectTextStringand‘hello’ininitializerClickOK.
Note: Astringisasetoflettersorcharacters.Aspaceisconsideredacharactersobecarefulifyouhaveaspaceattheendofyourwordorsentence.
Inthepenguin'sfunctions,draganddropthegetStringFromUser overthe‘hello’string.
Typein:Enteracronym.
Step 6: Question #3Continued...
NowwewilladdinourresponsestotheanswerinanotherIf/Elsestatement.
DraganddropanIf/Else statementandselecttrue.
ClickthedownarrowintrueandselectTextStringComparison->???equalsIgnoreCase???-> answerString-> CustomTextString thentypein:LSRC
WithequalsIgnoreCase,theanswerisnotcase-sensitive
Step 6: Question #3Continued...
Clickonpenguinintheobjecttreeandaddintheresponsesforwhentheansweriscorrectorincorrectasshownbelow.
Nowplayyourworldandtakethequiz.
Step 6: Question #3Continued...
Part 2: Forcing the Answer
InQuestion1,iftheusermistypestheanswer,wewanttheprogramtoasktheuserfortheansweragainuntilitiscorrect.Rewritequestion1toaddawhileloopandforcetheuserintotheloopwithawrongvaluetostartwith.
Part 3: Limiting Number of Trials
Tolimitthenumberoftrialsto3forexample,createanewvariablenamednumberofTrial,createawhileloopthatallowsthequestiontoshowupaslongasthestudentstrieslessthan3times.Eachtimetheyanswerincorrectly,incrementnumberofTrial by1.Iftheyanswer,correctly,setnumberofTrial toanumbergreaterthan3tobreakoutofthewhileloop(herewechoose5)
Part 4: Score Reporting
Challenge:Toreportscoretouser,wehavetocreatealocalvariablescore forthequizprocedure,initializeitto0,andincrementitby1everytimethestudentansweraquestioncorrectly.Attheend,havethepenguinreportthescore.
Toreportthescoreintheend,dragthepenguinsayprocedureinandtype“Yougot”.
Clickonthedownarrownexttothestringyoujusttypedandselect“Yougot“+???->WholeNumber->score
Part 4: Score Reporting
Clickonthedownarrownexttothe“Yougot“+scorestringandselect“Yougot“+score+??? ->CustomTextString andType”questionscorrect”
Part 4: Score Reporting
Quiz Procedure code