Programming with Python Midterm

Pataasin ang iyong marka sa homework at exams ngayon gamit ang Quizwiz!

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.

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

Assume you have a list of numbers 12, 10, 32, 3, 66, 17, 42, 99, 20 Write a loop that prints each of the numbers on a new line. Write a loop that prints each number and its square on a new line. Save & Run

nums = [12, 10, 32, 3,66,17,42,99,20] for i in nums: print(i, i**2)

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.

print("this program computes miles driven") miles_driven = input("enter miles driven") miles_driven = float(miles_driven) gallons_used = input("enter gallons used") gallons_used = float(gallons_used) mpg = miles_driven / gallons_used print(mpg)

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.

width = int(input("enter width here")) height = int(input("enter height here")) area = (width * height) print(area)

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

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

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

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

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

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

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

deg_f = int(input("the temperature in farenheit is")) deg_c = (5/9)* deg_f print(deg_c)

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 ...

for amonth in ["January", "February", "March", "April"]: print("one of the months of the year is", amonth)

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

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)

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.

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

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

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

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.

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)


Kaugnay na mga set ng pag-aaral

Kinesiology 171 Chapter 9 (Week 5)

View Set

Українська філософія

View Set

Morala - obiectul de studiu al eticii

View Set

Prep-u: Chapter 10: Health Assessment of Children

View Set