Docsity

code hs karel cheat sheet, Cheat Sheet of Software Development

Toggle sidebar
bg1
KarelCheatSheet
OverviewofProgrammingFunctionality
ProgrammingMode1
●go…moveonestepforward.Note:Ifrobotcrashesintoawall,anerroristhrown
●right…turn90degreesright
●left…turn90degreesleft
●get…pickupagem.Note:Theremustbeagembeneaththerobot,oranerroris
thrown
●put…dropsagemontheground.Note:Erroristhrownifrobot’sbagisempty
●repeat…repeatacommandorsequenceofcommandsagivennumberoftimes
ProgrammingMode2
●ifelse…conditions
● sensors...wall,gem,north,empty,home
●not…negation
●while…dosomethingwhileconditionissatisfied
●def…definecustomcommands
ProgrammingMode3
● print…printcommandsameasinPython
○ forexampleprint“HelloWorld!”,orprint“a=”,a
●gpsx,gpsy…GPScoordinates,canbeassignedtoavariable
● integerandlogicalvariables…forexample,a=0createsanewvariableaand
initializesitwithzero
●inc(a),inc(a,value)…increasethevalueofvariableabyoneorvalue
●dec(a),dec(a,value)…decreasethevalueofvariableabyoneorvalue
●rand…returnsrandomlyTrueorFalse
●return…canbeusedincustomcommandstoreturnvalue
●and,or…logicaland,ortobeusedinconditions,whileloops,andlogicalvariables
● lists…functionalityrelatedtolistsisthesameasinPython
○L=[]createsanemptylistL
○len(L)…lengthoflistL
○L[i]…iteminlistLatpositioni.Note:Indexingstartsfromzero
○L.append(x)…appenditemxattheendoflistL
○x=L.pop()…removethelastiteminlistLandreturnit
○delL[i]…deleteitematpositioniinlistL
Discount

On special offer

Partial preview of the text

Download code hs karel cheat sheet and more Cheat Sheet Software Development in PDF only on Docsity!

Karel Cheat Sheet

Overview of Programming Functionality

Programming Mode 1

● go… move one step forward. Note: If robot crashes into a wall, an error is thrown ● right… turn 90 degrees right ● left … turn 90 degrees left ● get … pick up a gem. Note: There must be a gem beneath the robot, or an error is thrown ● put… drops a gem on the ground. Note: Error is thrown if robot’s bag is empty ● repeat … repeat a command or sequence of commands a given number of times

Programming Mode 2

● if else… conditions ● sensors ... wall, gem, north, empty, home ● not … negation ● while… do something while condition is satisfied ● def … define custom commands

Programming Mode 3

● print … print command same as in Python ○ for example print “Hello World!”, or print “a =”, a ● gpsx, gpsy … GPS coordinates, can be assigned to a variable ● integer and logical variables … for example, a = 0creates a new variable a and initializes it with zero ● inc(a), inc(a, value)… increase the value of variable aby one or value ● dec(a), dec(a, value)… decrease the value of variable aby one or value ● rand… returns randomly True or False ● return… can be used in custom commands to return value ● and, or… logical and, or to be used in conditions, while loops, and logical variables ● lists … functionality related to lists is the same as in Python ○ L = [] creates an empty list L ○ len(L)… length of list L ○ L[i]… item in list Lat position i. Note: Indexing starts from zero ○ L.append(x)… append item xat the end of list L ○ x = L.pop() … remove the last item in list Land return it ○ del L[i] … delete item at position i in list L