Simple Assignments¶
-
Write code to create a variable
scoreand make it refer to the integer 1.Remember that an assignment is a=with a variable on the left-hand side and a value or expression on the right-hand side.
-
Write code to create a variable
temperatureand make it refer to a floating point number.A floating point number is a real number. I.e., it has a.in it.
-
Write code to create a variable
nameand make it refer to a string.A string is enclosed by single or double quotes.
-
Write code to create a variable
matchesand make it refer to a boolean value.The two booleans values areTrueandFalse.
-
Assume the variable
temperaturehas been initialized to some value. Write code to add 1 to the variabletemperature.The right-hand side of an equation is evaluated first, so you can taketemperatureand add 1 to it on the right-hand side, and then puttemperatureon the left-hand side of the=sign.
-
Assume you have two variables
hoursandhourly_rate. Write code to compute thetotal_pay, by multiplyinghoursandhourly_rateand storing intotal_pay.Multiplyhoursandhourly_rateon the right-hand side of the assignment statement.
-
Assume you have two variables
p1_pointsandp2_points. Write a line of code to indicate that player p1 added all of player p2’s points to her own.Your code needs to addp2_pointstop1_pointsand makep1_pointsrefer to the result.
-
Assume you have a variable
ball1_direction. Write a line of code that set ball2’s direction to be the same as ball1’s direction.Your code creates variableball2_directionon the left-hand side of the=sign and sets it value toball1_direction.
-
Replace these statements with a single statement so that you don’t use the variable
y– just the variablexbeing set to a value.
-
Before running the following code, predict what value will be printed.
If you don’t understand the answer, using CodeLens might help.