0

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
asked Oct 9, 2019 at 1:41
2
  • 5
    print("myCurrentAge is" + str(myCurrentAge) is missing the second closing bracket. You'll also want to use integers as opposed to strings. Commented Oct 9, 2019 at 1:44
  • 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. Commented Oct 9, 2019 at 1:47

2 Answers 2

1

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))
answered Oct 9, 2019 at 1:48
Sign up to request clarification or add additional context in comments.

7 Comments

okay thank you but when i do that i get TypeError: can only concatenate str (not "int") to str , i try to fix it changing the int to str and the print too but it keeps saying that
@karatekid, on what line is the error given? Try using my print line or try RachayitaGiri's suggestion.
the line is myNewAge = str(myCurrentAge) + (YEAR - currentYear)
@karatekid, because its not str(), it should be int(). Look at my answer very carefully.
okay, well i try to change it back to int() but it keeps saying that, my code is YEAR = 2050 myCurrentAge = 18 currentYear = 2019 print( "myCurrentAge is" + str(myCurrentAge)) myNewAge = int(myCurrentAge) + (int(YEAR) - int(currentYear)) #then print, print("I will be " + int(myNewAge) + " in YEAR" + YEAR)
|
0

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
answered Oct 9, 2019 at 1:52

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.