Practice Exercises for Variables and Assignments
Solve
each of the practice exercises below. Each problem includes three
CodeSkulptor links: one for a template that you should use as a starting
point for your solution, one to our solution to the exercise, and one
to a tool that automatically checks your solution.
- Given a template that pre-defines a variable , write an assignment statement that defines a variable whose value is the number of feet in miles. Miles to feet template --- Miles to feet solution --- Miles to feet (Checker)
- Given a template that pre-defines three variables , and , write an assignment statement that updates the variable to have a value corresponding to the total number of seconds for hours, minutes and seconds. Hours to second template --- Hours to second solution --- Hours to second (Checker)
- Given a template that pre-defines the variables and that are the lengths of the sides of a rectangle, write an assignment statement that defines a variable whose value is the perimeter of the rectangle in inches. Perimeter of rectangle template --- Perimeter of rectangle solution --- Perimeter of rectangle (Checker)
- Given a template that pre-defines the variables and that are the lengths of the sides of a rectangle, write an assignment statement that defines a variable whose value is the area of the rectangle in square inches. Area of rectangle template --- Area of rectangle solution --- Area of rectangle (Checker)
- Given a template that pre-defines the constant and the variable corresponding to the radius of a circle in inches, write an assignment statement that defines a variable whose value is the circumference of a circle with radius in inches. Circumference of circle template --- Circumference of circle solution --- Circumference of circle (Checker)
- Given a template that pre-defines the constant and the variable corresponding to the radius of a circle in inches, write an assignment statement that defines a variable whose value is the area of a circle with radius in square inches.Area of circle template --- Area of circle solution --- Area of circle (Checker)
- Given the pre-defined variables , and , write an assignment statement that define a variable whose value is dollars invested at percent interest, compounded annually for years. Future value template --- Future value solution --- Future value (Checker)
- Give the pre-defined variables and , write an assignment statement that defines the variable whose value is the string "My name is % %." where the percents should be replaced by and . Note that, in Python, you can use the operator on strings to concatenate (i.e. join) them together into a single string. Name tag template --- Name tag solution --- Name tag (Checker)
- Given the pre-defined variables (a string) and (a number), write an assignment statement that defines a variable whose value is the string where the percents should be replaced by and the string form of . Name and age template --- Name and age solution --- Name and age (Checker)
- Given the variables , , , and , write an assignment statement that defines a variable whose values is the distance between the points and . Point distance template --- Point distance solution --- Point distance (Checker)
- Challenge: Heron's formulastates the area of a triangle is where , and are the lengths of the sides of the triangle and is thesemi-perimeterof the triangle. Given the variables , , ,, , and , write a Python program that computes a variable whose value is the area of the triangle with vertices , and . (Hint: our solution uses five assignment statements.) Triangle area template --- Triangle area solution --- Triangle area (Checker)