In [1]:
price = 0.9
# Print the header
print('Balls: Price: Balls: Price:')
# Print prices in two columns
for balls in range(1,11):
left = balls
right = balls + 10
skeletton = '{:^6} {:^6.2f} {:^6} {:^6.2f}'
print(skeletton.format(left, left*price, right, right*price))
In [2]:
def get_category(bmi):
'''
A function which returns the category for a given bmi
'''
if bmi < 15.0:
return 'Very severely underweight'
elif bmi < 16.0:
return 'Severely underweight'
elif bmi < 18.5:
return 'Underweight'
elif bmi < 25.0:
return 'Normal'
elif bmi < 30.0:
return 'Overweight'
elif bmi < 35.0:
return 'Moderately obese'
elif bmi < 40.0:
return 'Severely obese'
else:
return 'Very severely obese'
In [3]:
def get_bmi(weight, height):
'''
This function takes the weight in [kg] and the height in [m]
It return the BMI.
'''
return weight / height**2
In [4]:
def bmi_calculator(weight=None, height=None):
'''
A calculator which takes weight and heigt.
It prints the bmi and corresponding category.
If no or only one value is given it asks for both.
'''
if weight == None or height == None:
print('This is a BMI calculator.')
weight = float(input('Enter your weight (kg): '))
height = float(input('Enter your height (m): '))
else:
status = 'Your weight is: {}\nYour height is: {}'
print(status.format(weight, height))
bmi = get_bmi(weight, height)
category = get_category(bmi)
skeletton = 'Your BMI is {:.1f}\nYour BMI category is {}'
print(skeletton.format(bmi, category))
bmi_calculator(75, 1.74)
print('\n')
bmi_calculator(49, 1.85)
print('\n')
bmi_calculator(128, 1.71)
In [7]:
bmi_calculator(80,1.73)
In [8]:
def primes(limit):
'''
A function implementing the sieve of erathostenes.
It take an upper limit and returns all prime numbers
under this limit.
'''
status = [True] * limit
status[0] = status[1] = False
primes = []
for (i, isprime) in enumerate(status):
if isprime:
primes.append(i)
for n in range(i*i, limit, i):
status[n] = False
return primes
In [12]:
primes(57140)[-2]
Out[12]:
In [6]:
max_num = int(input('Calculate prime numbers under: '))
for n in primes(max_num):
print(n, end = " ")
To make this even faster, you could exclude all even numbers from the beginning on and start with 2 in the list of primes.