-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDay 12 - count_dots.py
65 lines (44 loc) · 2.06 KB
/
Day 12 - count_dots.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
"""
Created on Tue Apr 05 2023
@author: GRACE ESTRADA
Day 12 of 50 Days of Python
Write a function called count_dots.
This function takes a string separated by dots as a parameter and counts how many dots are in the string.
For example, ‘h.e.l.p.’ should return 4 dots, and ‘he.lp.’ should return 2 dots.
"""
#Define count_dots function
def count_dots(str):
count = str.count('.')
return count
count_dots('h.e.l.p')
"""
EXTRA CHALLENGE: Your Age in Minutes
Write a function called age_in_minutes that tells a user how old they are in minutes.
Your code should ask the user to enter their year of birth, and it should return their age in minutes (by subtracting their year of birth to the current year).
Here are things to look out for:
a. The user can only input a 4 digit year of birth.
For example, 1930 is a valid year.
However, entering any number longer or less than 4 digits long should render input invalid.
Notify the user to input a four digits number.
b. If a user enters a year before 1900, your code should tell the user that input is invalid.
If the user enters the year after the current year, the code should tell the user, to input a valid year.
The code should run until the user inputs a valid year.
Your function should return the user's age in minutes.
For example, if someone enters 1930, as their year of birth your function should return:
You are 48,355,200 minutes old.
"""
#Define age_in_minutes function
import datetime
def age_in_minutes():
while True:
year = input("Please enter your year of birth (four digits): ")
if len(year) != 4:
print("Invalid input. Please enter a four-digit year.")
elif int(year) < 1900 or int(year) > datetime.datetime.now().year:
print("Invalid input. Please enter a valid year.")
else:
break
age_in_years = datetime.datetime.now().year - int(year)
age_in_minutes = age_in_years * 365 * 24 * 60
print("You are {} minutes old.".format(age_in_minutes))
age_in_minutes()