ASSIGNMENT 2
Week 2
Computer Science for Designers and Artists
1. Write a program to calculate the area of this triangle.
The formula to calculate the area of a right triangle is: area = 12 (base * height)
comment your code
use variables for the area, base and height, use good variable names
print the result
2. Determine the result of each of the following lines of code (either True or False) given the following values for x, y and z.
x = True y = False z = True
x and y
True or False
x and z
True or False
x or y
True or False
x and y or z
True or False
y or (x and y)
True or False
3. Write the output for the following block of code
x = 17 y=x*4 print (y)
4. State the bugs in this code
_length = 4
alex = Alex
and = and
er = er
short_name = alex/_length
print short_name
5. Is this number PRIME?
Prime numbers are awesome and will probably be on the test we get from intelligent aliens when they contact us. Knowing if a number is prime just might save the human race. Heres the definition of a prime: A Prime
Number has no divisors other than 1 and itself. And it must be a whole number greater than 1.
So wouldnt you know, weve just received a transmission of alien origin containing a number! Its go time lets see if it is a prime number. The number in question is in the variable: potential_prime. For each of the three conditions of being a prime number (listed below), show code that will give a value of True if it satisfies the condition or False if it does not.
Condition#1:
Is potential_prime greater than one?
Condition#2:
Is potential_prime a whole number?
Condition#3:
For every number between 1 and potential_prime (lets represent that with a variable named: index) Is potential_prime Indivisible by index?
6. Hack the dice rolling program
Load the dice.py program. Give the code a look to see if you can figure out what it does. Run it to roll the dice. Make the following changes to the code:
1) Change the program so you are using 3, twenty sided dice (like a wizard) instead of 5 six sided dice. Save this as dice_wizards.py and submit.
2) Go back to 5 six sided dice, but make it so that it only ever rolls even numbers (2,4 and 6). Save this as dice_evens.py and submit.
Reviews
There are no reviews yet.