Simple Assignments¶
-
Write code to create a variable
score
and 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
temperature
and 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
name
and make it refer to a string.A string is enclosed by single or double quotes.
-
Write code to create a variable
matches
and make it refer to a boolean value.The two booleans values areTrue
andFalse
.
-
Assume the variable
temperature
has 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 taketemperature
and add 1 to it on the right-hand side, and then puttemperature
on the left-hand side of the=
sign.
-
Assume you have two variables
hours
andhourly_rate
. Write code to compute thetotal_pay
, by multiplyinghours
andhourly_rate
and storing intotal_pay
.Multiplyhours
andhourly_rate
on the right-hand side of the assignment statement.
-
Assume you have two variables
p1_points
andp2_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_points
top1_points
and makep1_points
refer 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_direction
on 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 variablex
being 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.