Sie sind auf Seite 1von 3

# SUBTASK 1: Replacing if/break with a loop condition

n = 10
while True:
print("*"*n)
n -= 1
if n < 0:
break

# TODO: write an equivalent while loop with a proper condition instead of True

n = 10
while n > 0:
print("*"*n)
n -= 1

# TODO: rewrite the code to stop after a several iterations

# changing loop condition


while 10 < n < 50:
print("*"*n)
n += 1
time.sleep(0.25)

Bachelor

# Fortune-teller
# Author: Carmine Catalano TODO: Fortune-teller

"""
This is our simple fortune-teller.

Example:
>>> bachelor_age_estimation(20, bachelor_semesters_estimation(2, 60) - 2)
22
"""

def sanitize_parameter(a_number, a_name, a_min=0):


"""
Returns a number (param) if it is an integer greater or equal to a min (param).
Otherwise, it raises ValueError for a variable of a given name (param).

:param a_number: a given number


:type; int

:param a_name: a given string named var


:type a:name: str

:param a_min: a given number whose value is min 0


:type a_min: int

:return: a_number
:rtype: int

Examples:
>>> sanitize_parameter(15, "var")
15
>>> sanitize_parameter(15, "var", 17)
Traceback (most recent call last):
...
ValueError: var must be an integer and var > 17
>>> sanitize_parameter(1.0, "var")
Traceback (most recent call last):
...
ValueError: var must be an integer and var > 0
>>> sanitize_parameter("s", "var")
Traceback (most recent call last):
...
ValueError: var must be an integer and var > 0
>>> sanitize_parameter(True, "var")
Traceback (most recent call last):
...
ValueError: var must be an integer and var > 0
"""
if type(a_number) != int or a_number <= a_min:
raise ValueError(a_name + " must be an integer and " + a_name + " > " +
str(a_min))
return a_number

def bachelor_semesters_estimation(a_semesters, a_points):


"""
Predict how many semesters are needed to finish a bachelor degree
assuming a steady peace of gaining ECTS points.

:param a_semesters: a given positive number of semesters already attended


:type a_semesters: int

:param a_points: a positive number of ECTS points gained in a_semesters


:type a_points: int

:return: a positive number of semesters estimation


:rtype: int

Examples:
>>> bachelor_semesters_estimation(1, 30)
6
>>> bachelor_semesters_estimation(2, 30)
12
>>> bachelor_semesters_estimation(2, 7)
52
>>> bachelor_semesters_estimation(2, 7)
52
>>> bachelor_semesters_estimation(4, 180)
4
>>> bachelor_semesters_estimation(8, 240)
8
>>> bachelor_semesters_estimation(1, 0)
Traceback (most recent call last):
...
ValueError: a_points must be an integer and a_points > 0
>>> bachelor_semesters_estimation(0, 1)
Traceback (most recent call last):
...
ValueError: a_semesters must be an integer and a_semesters > 0
"""
import math
sanitize_parameter(a_semesters, "a_semesters")
sanitize_parameter(a_points, "a_points")
if a_points > 180:
return a_semesters
semesters_estimation = math.ceil(180 / (a_points / a_semesters))
return semesters_estimation

def bachelor_age_estimation(a_age, a_semesters):


"""
Calculate the age of a bachelor student after a given number of
semesters adding the number of semesters to a given age.

:param a_age: a given number of years


:type a_age: int

:param a_semesters: a given number of semesters


:type a_semesters: int

:return: age estimation


:rtype: int

Examples:
>>> bachelor_age_estimation(44, 10)
49
>>> bachelor_age_estimation(22, 3)
24
>>> bachelor_age_estimation("22", 3)
Traceback (most recent call last):
...
ValueError: a_age must be an integer and a_age > 17
>>> bachelor_age_estimation(22, "3")
Traceback (most recent call last):
...
ValueError: a_semesters must be an integer and a_semesters > 0
>>> bachelor_age_estimation(17, 3)
Traceback (most recent call last):
...
ValueError: a_age must be an integer and a_age > 17
>>> bachelor_age_estimation(27, 0)
Traceback (most recent call last):
...
ValueError: a_semesters must be an integer and a_semesters > 0
"""
sanitize_parameter(a_age, "a_age")
sanitize_parameter(a_semesters, "a_semesters")
age_estimation = round(a_age + (a_semesters / 2))
return age_estimation

if __name__ == '__main__':
import doctest
doctest.testmod()

Das könnte Ihnen auch gefallen