Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
6 views

Python Practice

Uploaded by

gisselle071
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

Python Practice

Uploaded by

gisselle071
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

1.

(5 Points) Write a function find_circle_circumference() that takes the radius


of a circle as its only parameter and returns its circumference.

import math

def find_circle_circumference(radius):
""" Calculate the circumference of a circle given its
radius. """
circumference = 2 * math.pi * radius
return circumference

2. (5 Points) Write a function print_char(s) that takes one parameter: a string s. This
function should print out each character in s unless the character is a whitespace. Include
a docstring for the function.

def print_char(s):
""" Prints each character in the given string s, except
whitespace characters. """

for char in s:
if char != " ":
print(char)

1
3. (5 Points) Write a function print_students() that takes two parameters: a
dictionary grades where the keys are student names (strings) and the values are their
grades (integers), and a character char. The function should print each student's name
and grade in the format name:grade (without spaces) if the student's name starts with
char. Include a docstring for the function.

def print_students(grades, char):


""" Prints each student's name and grade if the
student's name starts with the specified character """

for name, grade in grades.items():


if name[0] == char:
print(f"{name}:{grade}")

4. (10 Points)
a. Create a new class Student for students in a classroom. It has two attributes:
name (string) and grade (int). Then, create a constructor method
__init__() for the class, which should initiate the attribute name to
"unknown", and the attribute grade to 0.
b. Create a new object of the class and assign it to new_student
c. Change the value of attribute name of new_student to "Alice", and the
value of attribute grade to 80.
d. Create a new method add_bonus(bonus)for the class, which increases the
value of attribute grade by bonus.
e. Call the method add_bonus(bonus) on new_student to increase its
attribute grade by 5.

class Student:
""" A class representing a student with a name and
grade. """

def __init__(self):
self.name = "unknown"
self.grade = 0

def add_bonus(self, bonus):

2
self.grade += bonus

new_student = Student()
new_student.name = "Alice"
new_student.grade = 80
new_student.add_bonus(5)

You might also like