Year 2050

calendar-clockHave you got any idea how old you will be in 2050?

By completing the following challenges we will write computer algorithms for the computer to tell us more about our age in the future!

Getting Started


First you will need to copy the following code in your Python IDE or using an online Python editor.

thisYear = 2015 
print("We are in " + str(thisYear))
age = int(input("How old are you?"))
print("You are " + str(age) + " years old.")

Challenge #1: How old will you be in…


ChallengeSolution
Complete the above code with a for loop to display the following messages:

  • In 2016 you will be … years old.
  • In 2017 you will be … years old.
  • In 2050 you will be … years old.

thisYear = 2015 
print("We are in " + str(thisYear))
age = int(input("How old are you?"))
print("You are " + str(age) + " years old.")

for i in range(0,36):
  thisYear += 1
  age += 1
  print("In " + str(thisYear) + " you will be " + str(age) + " years old.")

Challenge #2: How old will you be in 2050 (without using a loop).


ChallengeSolution
Complete the above code without using a for loop to display the following message:

  • In 2050 you will be … years old.

thisYear = 2015 
print("We are in " + str(thisYear))
age = int(input("How old are you?"))
print("You are " + str(age) + " years old.")

yearsDifference = 2050 - thisYear
age = age + yearsDifference
print("In 2050 you will be " + str(age) + " years old.")

Challenge #3: When were your born?


ChallengeSolution
Tweak your code so that the Python program calculates and displays the year you were born. For instance it will display:

  • You were born in ….

thisYear = 2015 
print("We are in " + str(thisYear))
age = int(input("How old are you?"))
print("You are " + str(age) + " years old.")

birthYear = thisYear - age
print("You were born in " + str(birthYear) + ".")

Challenge #4: In how many years will you turn 21!


ChallengeSolution
Tweak your code so that the Python program calculates and displays how many years till your turn 21. Note that if the end-user is older than 21, then the program should tell them how many years have passed since their 21st birthday.

thisYear = 2015 
print("We are in " + str(thisYear))
age = int(input("How old are you?"))
print("You are " + str(age) + " years old.")

if age<21:
  yearsDifference = 21 - age
  print("You will be 21 in " + str(yearsDifference) + " years.")
elif age==21:
  print("You are 21 years old this year!")
elif age>21:
  yearsDifference = age - 21
  print("You were 21 years old " + str(yearsDifference) + " years ago.")

Extension


In all the challenges listed above we hardcoded the current year to 2015. This means that our programs will not work next year unless we change the code on line 1: from thisYear = 2015 to thisYear = 2016.

Instead we can improve our code by getting Python to find out what year we are currently in.

The following lines of code show you how to achieve this. You can reuse them to tweak your code.


unlock-access

Solution...

The solution for this challenge is available to full members!
Find out how to become a member:
➤ Members' Area

Did you like this challenge?

Click on a star to rate it!

Average rating 1.9 / 5. Vote count: 9

No votes so far! Be the first to rate this post.

As you found this challenge interesting...

Follow us on social media!

Tagged with: , ,