Programming with Python Midterm Flashcards

1
Q

Write a program that will compute the area of a rectangle. Prompt the user to enter the width and height of the rectangle. Print a nice message with the answer.

A
  • width = int(input(“enter width here”))
  • height = int(input(“enter width here”))
  • area = ( width * height)
  • print(area)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Write a program that will compute MPG for a car. Prompt the user to enter the number of miles driven and the number of gallons used. Print a nice message with the answer.

A
  • print( this program computes miles driven”)
  • miles_driven = input(miles_driven)
    -miles_driven = float(miles_driven)
    -gallons_used = input(gallons_used)
  • gallons_used = float (gallons_used)
  • mpg = miles_driven / gallons_used
  • print(mpg)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Write a program that will convert degrees fahrenheit to degrees celsius.

A
  • deg_f = int(input(“the temperature in farenheit is”))
  • deg_c = (5/9)* deg_f
  • print(deg_c)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Assume you have a list of numbers 12, 10, 32, 3, 66, 17, 42, 99, 20

a. Write a loop that prints each of the numbers on a new line.

b. Write a loop that prints each number and its square on a new line.
b.

A
  • nums = [12, 10, 32, 3,66,17,42,99,20]
  • for i in nums:
    • print(i, i**2)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Write a program that uses a for loop to print
One of the months of the year is January
One of the months of the year is February
One of the months of the year is March
etc …

A
  • for amonth in [“January”, “February”, “March”, “April”]:
    • print(“one of the months of the year is”, amonth)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Write a fruitful function sumTo(n) that returns the sum of all integer numbers up to and including n. So sumTo(10) would be 1+2+3…+10 which would return the value 55. Use the equation (n * (n + 1)) / 2.

A

import turtle
def sumTo(n):
result = (n * (n + 1)) / 2
return result
t = sumTo(0)
print(“The sum from 1 to 0 is”,t)
t = sumTo(10)
print(“The sum from 1 to 10 is”,t)
t = sumTo(5)
print(“The sum from 1 to 5 is”,t)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Write a function areaOfCircle(r) which returns the area of a circle of radius r. Make sure you use the math module in your solution.

A

import math
def areaOfCircle(r):
return math.pi * r * r
print(areaOfCircle(10))

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Rewrite the function sumTo(n) that returns the sum of all integer numbers up to and including n. This time use the accumulator pattern

A

def sumTo(n):
total = 0
for i in range (1, n+1):
total = total + i
return total
print(sumTo(10))

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Modify is_odd so that it uses a call to is_even to determine if its argument is an odd integer.

A

from test import testEqual

def is_even(n):
if n % 2 == 0:
return True
else:
return False

def is_odd(n):
if is_even(n):
return False
else:
return True

testEqual(is_odd(10), False)
testEqual(is_odd(5), True)
testEqual(is_odd(1), True)
testEqual(is_odd(0), False)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Now write the function is_odd(n) that returns True when n is odd and False otherwise.

A

import random
def is_odd(n):
if n % 2 == 0:
return False
else:
return True

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Write a function called is_even(n) that takes an integer as an argument and returns True if the argument is an even number and False if it is odd.

A

def is_even(n):
if n % 2 == 0:
return True
else:
return False

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Write a function, is_prime, that takes a single integer argument and returns True when the argument is a prime number and False otherwise.

A

def is_prime(n):
i=0
c=0
for i in range(1,n+1):
if n%i==0:
c+=1
if c>2:
return False
return True

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Write a function print_triangular_numbers(n) that prints out the first n triangular numbers. A call to print_triangular_numbers(5) would produce the following output:

1 1
2 3
3 6
4 10
5 15

A

def is_prime(n):
x = n - 1

if n < 2:
    return False
    

if n == 2:
    return True
    
while x > 1:
    if n % x == 0:
        return False
    else:
        x -= 1       
return True
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Use a for statement to print 10 random numbers.

A

import random

rand_list=[]
n=10
for i in range(n):
rand_list.append(random.randint(3,9))
print(rand_list)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Write a function findHypot. The function will be given the length of two sides of a right-angled triangle and it should return the length of the hypotenuse. (Hint: x ** 0.5 will return the square root, or use sqrt from the math module)

A

def findHypot(a,b):
c = a2 + b2
return c**0.5

def main():
a = float(input(‘Enter the value of a: ‘))
b = float(input(‘Enter the value of b: ‘))
hypo = findHypot(a, b)
print(‘The hypotenuse is ‘, hypo)

if __name__ ==
main()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Write a function findHypot. The function will be given the length of two sides of a right-angled triangle and it should return the length of the hypotenuse. (Hint: x ** 0.5 will return the square root, or use sqrt from the math module)

A

def findHypot(a,b):
c = a2 + b2
return c**0.5

def main():
a = float(input(‘Enter the value of a: ‘))
b = float(input(‘Enter the value of b: ‘))
hypo = findHypot(a, b)
print(‘The hypotenuse is ‘, hypo)

if __name__ ==
main()