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.

  1. Given a template that pre-defines a variable miles, write an assignment statement that defines a variable feetwhose value is the number of feet in miles miles. Miles to feet template --- Miles to feet solution --- Miles to feet (Checker)
  2. Given a template that pre-defines three variables hours, minutes and seconds, write an assignment statement that updates the variable total_seconds to have a value corresponding to the total number of seconds for hours hours, minutes minutes and seconds seconds. Hours to second template --- Hours to second solution ---Hours to second (Checker)
  3. Given a template that pre-defines the variables width and height that are the lengths of the sides of a rectangle, write an assignment statement that defines a variable perimeter whose value is the perimeter of the rectangle in inches. Perimeter of rectangle template --- Perimeter of rectangle solution --- Perimeter of rectangle (Checker)
  4. Given a template that pre-defines the variables width and height that are the lengths of the sides of a rectangle, write an assignment statement that defines a variable area whose value is the area of the rectangle in square inches. Area of rectangle template --- Area of rectangle solution --- Area of rectangle (Checker)
  5. Given a template that pre-defines the constant PI and the variable radius corresponding to the radius of a circle in inches, write an assignment statement that defines a variable circumference whose value is the circumference of a circle with radius radius in inches. Circumference of circle template --- Circumference of circle solution --- Circumference of circle (Checker)
  6. Given a template that pre-defines the constant PI and the variable radius corresponding to the radius of a circle in inches, write an assignment statement that defines a variable area whose value is the area of a circle with radius radius in square inches.Area of circle template --- Area of circle solution --- Area of circle (Checker)
  7. Given the pre-defined variables present_value, annual_rate and years, write an assignment statement that define a variable future_value whose value is present_value dollars invested at annual_rate percent interest, compounded annually for years years. Future value template --- Future value solution --- Future value (Checker)
  8. Give the pre-defined variables first_name and last_name, write an assignment statement that defines the variable name_tag whose value is the string "My name is % %." where the percents should be replaced by first_name and last_name. 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)
  9. Given the pre-defined variables name (a string) and age (a number), write an assignment statement that defines a variable statement whose value is the string "% is % years old." where the percents should be replaced by name and the string form of age. Name and age template --- Name and age solution --- Name and age (Checker)
  10. Given the variables x0, y0, x1, and y1, write an assignment statement that defines a variable distance whose values is the distance between the points (x0,y0) and (x1,y1). Point distance template --- Point distance solution --- Point distance (Checker)
  11. Challenge: Heron's formulastates the area of a triangle is s(sa)(sb)(sc)−−−−−−−−−−−−−−−−−√ where a, b and c are the lengths of the sides of the triangle and s=12(a+b+c) is thesemi-perimeterof the triangle. Given the variables x0, y0, x1,y1, x2, and y2, write a Python program that computes a variable area whose value is the area of the triangle with vertices (x0,y0), (x1,y1) and (x2,y2). (Hint: our solution uses five assignment statements.) Triangle area template --- Triangle area solution --- Triangle area (Checker)

En poursuivant votre navigation sur mon site, vous acceptez l’utilisation des Cookies et autres traceurs  pour réaliser des statistiques de visites et enregistrer sur votre machine vos activités pédagogiques. En savoir plus.