karatekid
karatekid

Reputation: 3

What am I doing wrong with this?

first of all, i'm very new at programming and starting my courses in college, i'm still trying to get the hang of it, please be nice. // i'm doing homework and it's this thing about calculating my age in the year of 2050, i've followed the instructions and everything and i get a syntax error in the print("I will be") line, can anyone tell me why and what am i doing wrong?

YEAR = "2050"
myCurrentAge = "18"
currentYear = "2019"
print("myCurrentAge is" + str(myCurrentAge)   
myNewAge = myCurrentAge + (YEAR - currentYear)
print("I will be" + str(myNewAge) + "in YEAR.")
stop

Upvotes: 0

Views: 2585

Answers (2)

CodeRed
CodeRed

Reputation: 903

First, as @ObsidianAge pointed out, your print("myCurrentAge is" + str(myCurrentAge) line is missing the second closing parenthesis, it should be like this print("myCurrentAge is" + str(myCurrentAge)).

Next is the error that you are talking about. You are calculating here myNewAge = myCurrentAge + (YEAR - currentYear) a bunch of string variables. You need to parse first your variables into int like this :

myNewAge = int(myCurrentAge) + (int(YEAR) - int(currentYear))
#then print,
print("I will be " + str(myNewAge) + " in YEAR" + YEAR)

To solve the confusion in the comments and I saw that you are following this answer's suggestion so here's the entire code:

# int variables
YEAR = 2050
myCurrentAge = 18
currentYear = 2019
# printing current age
print( "myCurrentAge is " + str(myCurrentAge))
# computing new age, no need to parse them to int since they already are
myNewAge = myCurrentAge + (YEAR - currentYear)
# then printing, parsing your then int vars to str to match the entire string line
print("I will be " + str(myNewAge) + " in YEAR " + str(YEAR))

Upvotes: 1

Rachayita Giri
Rachayita Giri

Reputation: 487

Also, after you fix the bracket thing mentioned by @ObsidianAge, you will have to fix another thing.

All your variables are Strings right now since you have declared them within double-quotes. To be able to do what you intend to do, you will need to convert them to integers.

I'd recommend assigning them integer values in the very beginning by removing the quotes. With your current code, it will just concatenate all your variable strings together when you use the + operator. So your new declarations will look like this:

YEAR = 2050
myCurrentAge = 18
currentYear = 2019

Upvotes: 0

Related Questions