Python Forum
Calculating what year it is when you are 100 years old
Thread Rating:
  • 1 Vote(s) - 5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Calculating what year it is when you are 100 years old
#1
I'm trying to figure out how to get the program to calculate what year it is when the user is 100 years old.

The error that it gives me is: Traceback (most recent call last): TypeError: unsupported operand type(s) for -: 'int' and 'str'

[code] print("The date is as follows:")
    import datetime
    now = datetime.datetime.now()
    print (now.year, now.month, now.day)

    name = input("Hi. Please enter your name: ")


    print("Hello", name)
    age = input("Please enter your age: ")
    print("You are going to be age 100 on the year", (now.year-age+100))[/code]
Reply
#2
input returns string, you have to convert it to integer before calculation
Test everything in a Python shell (iPython, Azure Notebook, etc.)
  • Someone gave you an advice you liked? Test it - maybe the advice was actually bad.
  • Someone gave you an advice you think is bad? Test it before arguing - maybe it was good.
  • You posted a claim that something you did not test works? Be prepared to eat your hat.
Reply
#3
(Apr-26-2017, 08:00 PM)volcano63 Wrote: input returns string, you have to convert it to integer before calculation

Thank you... I can't believe I didn't see that -_-
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  havent programmed in years - confused by why RETURN is not returning stmoose 5 1,128 Mar-26-2023, 05:51 AM
Last Post: buran
  Trying to get year not the entire year & time mbrown009 2 862 Jan-09-2023, 01:46 PM
Last Post: snippsat
  'Age' categorical (years -months -days ) to numeric Smiling29 4 2,874 Oct-17-2019, 05:26 PM
Last Post: Smiling29
  Create a monthly mean column in multiple years fyec 1 3,973 Jun-21-2018, 03:57 AM
Last Post: scidam
  Replacing missing years in the dataframe sangeethasivakumar 0 1,948 Mar-17-2018, 06:31 AM
Last Post: sangeethasivakumar

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020