Lecture 4 — Exercises

Solutions to the problems below must be sent to Submitty for grading. A separate file must submitted for each problem. These must be submitted by 4 pm on Tuesday, January 30.

  1. Write a program that assigns a string to the variable called phrase and then transforms phrase into a hashtag. In other words, all words in phrase are capitalized, all spaces are removed, and a # appears in front. Store the result in a variable called hashtag. Then print the value of both phrase and hashtag. Your program should start with

    phrase = 'Things you wish you knew as a freshman'
    

    and the output from the program (using print() function calls) should be

    The phrase "Things you wish you knew as a freshman"
    becomes the hashtag "#ThingsYouWishYouKnewAsAFreshman"
    

    Note that the output includes the quotation marks.

  2. One of the challenges of programming is that there are often many ways to solve even the simplest problem. Consider computing the area of the circle with the standard formula

    \[a(r) = \pi r^2\]

    Fortunately, we now have pi from the math module, but to compute the square of the radius we now can use ** or pow() or we can just multiply the radius times itself. To print the area accurate to only a few decimal places we can now use the string format() method or the round() built-in function, which includes an optional second argument to specify the number of decimal places.

    Write a short Python program that computes and prints the areas of two circles, one with radius 5 and the other with radius 32. Your code must use ** once and pow() once and it must use format() once and round() once. The output should be exactly

    Area 1 = 78.54
    Area 2 = 3216.99