Not logged in. Login

Exercise 2

Errors

Create a text file errors.txt containing your answers to this part. The following are attempts at writing a Python statement. For each one, is it valid (i.e. will it execute with no errors)? If there is an error, why (as an English sentence describing the actual problem, not a copy of an error message)? Try to figure out the answer for each before you check it in the interpreter.

Assume that the interpreter has just been started (no variables are defined) and we attempt these "statements" in this order.

one hundred = 100
pi = 3.1994
3.1416 = pi
seven = six + 1
print(100/2/5)
print('100/2/5')
print(x=10)
print("Hello, world)

Circles

Create a Python program named circle.py that calculates the area of a circle for the user. Here are two examples of what the program should look like when you run it (where "10" and "39.43" are user input):

Enter the radius of the circle: 10
The area of the circle is 314.16.
Enter the radius of the circle: 39.43
The area of the circle is 4884.32374584.

Use the formula \(3.1416r^2\) to calculate the area.

Modify the program so it will refuse to run if the user enters a radius that is less than zero:

Enter the radius of the circle: -10
Sorry, I can't do a negative radius.
Enter the radius of the circle: 39.43
The area of the circle is 4884.32374584.

It should still do the same calculation for values of zero or more. Hint:

if radius < 0:

Binary Integers

In a text file binary.txt, convert each of these unsigned binary values to decimal integers. Show the calculation you did to get the decimal value.

  • 00110011
  • 11111111
  • 01111111
  • 10010110

Do the same treating the values as signed (two's complement) integers.

Submitting

Submit all of your work through CourSys.

Updated Thu May 09 2024, 13:01 by ggbaker.