[SOLVED] CMPT120 Assignment 1

30.00 $

Category:
Click Category Button to View Your Next Assignment | Homework

You will receive the following solution file(s) instantly after successful payment:

zip file icon a1-u75nos.zip (54.8 KB)
Assignment Instructions Updated Recently? Submit Below and we will provide new Solution!
Submit New Instructions
🔒 Securely Powered by:
Secure Checkout
5/5 - (1 vote)

Assignment 1

This assignment is a set of practice problems to help you get familiar with Python and its environment.

Please copy the following files to your computer, and for each question write your answers in the corresponding file: q1.py, q2.py, q3.py, and q4.py. To get all these files at once, download a1_starting_files.zip and extract them to your computer as regular files.

When you are done, put q1.py, q2.py, q3.py, and q4.py into a .zip file named a1.zip, and submit just a1.zip on Canvas.

If you are unsure how to zip/unzip files, there is help online, e.g.: – how to zip and unzip files on Windows – how to zip and unzip files on a Mac

If you run into problems, please talk to a TA, or post a question to the Tech Support discussion in the 120 Canvas course.

Hint The notes at the end of this assignment explain f-strings and string arithmetic, which are needed in the questions.

Question 1

Put your answer to this question in q1.py.

Write a program that asks the user for their name and age, and then prints a hello message that also says how old they’ll be in 25 years. Use fstrings to print the output.

For example:

“` What’s your name? Elon How old are you? 51

Hi Elon! In 25 years you’ll be 76 years old. “`

You don’t need to do any error checking: assume a sensible name and integer age are entered.

Question 2

Put your answer to this question in q2.py.

Write a program that converts feet to meters. Ask the user to enter a measurement in feet, and then print out how much that is in meters:

How many feet? 21 21.0 is about 6.4 meters

Use f-strings to format your output. It should work for any number the user enters. Make your output the same as shown. The value for both the feet and meters should be printed to exactly 1 decimal place.

To convert feet $$f$$ to meters $$m$$, use the formula $$m = 0.3048 f$$.

You can assume the user enters a valid number.

Question 3

Put your answer to this question in q3.py.

Write a program that asks the user to enter a message, and also a box character, and prints the message in the box using the given box character. Print it in exactly the same style used in the examples.

For example:

“` What do you want your sign to say? Code Carefully What character do you want for the box? ! !!!!!!!!!!!!!!!!!! ! Code Carefully ! !!!!!!!!!!!!!!!!!! “`

Or:

“` What do you want your sign to say? SFU What character do you want for the box? %

%%%%%%% % SFU % %%%%%%% “`

Make sure your box has exactly the same format as shown. Note that there is one space before the message, and one space after the message.

Assume the user enters a single character for the box character.

Question 4

Put your answer to this question in q4.py.

You’re working for a company named Pool Master 3000, and they install backyard pools. They would like you to write a program that estimates the volume and cost of a pool given its dimensions.

Here’s a sample of how the program should work:

“` – Pool Master 3000 –

Enter all measurements are in meters.

How many meters wide is your pool? 3.1 How many meters long is your pool? 6.5 How many meters deep is your pool? 2

Volume of water: 40.3 cubic liters

Surface area of pool: 58.6 square meters

Lining, $42.50 per square meter: $2,488.38 “` Your program should do the following:

Print the title of the program at the top of the screen (in a box like the previous question), plus the message that all measurements are in meters.

Ask the user to enter the width, length, and depth of the pool. For simplicity, assume the entire pool has one depth. Also assume the user only enters sensible positive numbers.

The volume of the pool is printed to exactly 1 decimal digit. Use the formula $$v = wld$$, where $$v$$ is volume, $$w$$ is width, $$l$$ is length, and $$d$$ is depth.

The surface area of the pool is printed to exactly 1 decimal digit. The surface area is the area of the bottom of the pool, plus the four walls.

Use the formula $$s = wl + 2dl + 2dw$$, where $$s$$ is surface area, $$w$$ is width, $$l$$ is length, and $$d$$ is depth.

Print the cost of the lining to exactly 2 decimal digits. One square meter of lining costs \$42.50, and so the total lining cost is the surface area of the pool times \$42.50. Also, since the cost of the lining could be quite high, use commas to group the digits into threes, e.g. print 14,151.00 instead of 14151.00.

Marking Scheme

2 marks for question 1: 1 for correctness, 1 for using f-strings properly

2 marks for question 2: 1 for correctness, 1 for using f-strings properly

2 marks for question 3: 1 for correctness, 1 for using string arithmetic properly

6 marks for question 4: 1 mark for correctly implementing each bullet point, and 1 mark for beautiful output that is easy to read and understand

1 mark overall: consistent indentation and spacing (all blank lines and spaces should have a good reason for being there) 1 mark overall: all variable names are self-descriptive

1 mark overall: the length of all lines are less than, or equal to, 100 characters

Notes

Printing with f-strings

This program says hello to the user:

python name = input(“What’s your name? “) print(“Hello”, name, “!”) For example:

What’s your name? Elon Hello Elon !

Notice the space before the !. When you use commas in a print statement, they always add this extra space.

A more flexible way of printing in Python is to use an f-string:

python name = input(“What’s your name? “) print(f”Hello {name}!”)

This gets rid of the space:

What’s your name? Elon Hello Elon!

An f-string is a regular Python string that starts with f. Inside the string, any variable or expression that appears inside curly braces, {}, is evaluated and its result is put into the string at that position. An f-string is like a template, and in practice they are a nice way to make formatted strings.

Number precision with f-strings

Consider this program:

python x = 12.3456 print(f’x = {x}’)

It prints this:

x = 12.3456

There are 4 digits after the decimal point. But what if you only want 2 digits after the decimal? You can do that like this:

python x = 12.3456 print(f’x = {x:.2f}’)

This prints:

x = 12.35

Instead of {x} in the f-string, we wrote {x:.2f}. The .2 means to put exactly two digits after the decimal point, and the f means to format the number in regular floating point style. Notice also that the last digit has been rounded up.

If you want more digits after the decimal than the number has, 0s are added. For example:

python x = 12.3456 print(f’x = {x:.7f}’)

This prints:

x = 12.3456000

Three 0s were automatically added to make exactly 7 digits after the decimal.

Comma Grouping with f-strings

f-strings make it easy to write numbers in style that uses commas to group three digits together: “`python

cost = 1391.25 print(f’cost is ${cost:.2f}’) cost is $1391.25 print(f’cost is ${cost:,.2f}’) # note the , cost is $1,391.25 cost = 348355303.232 print(f’cost is ${cost:,.2f}’) # note the , cost is $348,355,303.23 “`

String Arithmetic

Here are some string functions that are needed in the next question: “`python

s = ‘hamster’ t = ‘food’ len(s) # the len function 7 # returns the number len(t) # of characters in a string 4 s + t # + adds two strings to ‘hamsterfood’ # make a new one s + ” ” + t ‘hamster food’

3 * t # * makes a new string by ‘foodfoodfood’ # repeating a string 3 * t + ‘!’ ‘foodfoodfood!’ 3 * (t + ‘!’)

‘food!food!food!’

s + ‘\n’ + len(s)‘-‘ # a neat trick for ‘hamster\n——-‘ # underlining a string print(s + ‘\n’ + len(s)‘-‘) hamster

“`

  • a1-u75nos.zip