1
 # this program says hello and ask my name.
 print('Hello World')
 print('Whats your name?')
 myname = input()
 print('its good to meet you, ' + myname) #ask for my name
 print('the length of your name is : ')
 print(len(myname))
 print('whats your age?')#ask for my age
 myage = input()
 print('you  will be ' + str(int(myage) + 1) + 1 'in a year')

I see syntax error at line 10 of the code i see all the parenthesis are correctly placed.

5 Answers5

1

I think this is what you want based on the logic of your program

print('you will be ' + str(int(myage) + 1) + ' in a year')

output:

Hello World
Whats your name?
Jon Doe
its good to meet you, Jon Doe
the length of your name is : 
7
whats your age?
23
you will be 24 in a year

Edit: OP wants me to explain when to use + in print(). usually if you are printing more than one arguments in your print() statement you want to join them using +. and when you join arguments together remember their type have to be the same. so for example. print(1 + '2') would give you an error because 1 is a int and '2' is a str. you would have to cast the arguments to make them the same. so print(1 + int('2')) would correctly give you 3

J.Doe
  • 179
  • 6
  • whats your age? 26 Traceback (most recent call last): File "C:/Users/punithsrk/Desktop/hello.py", line 10, in print('you will be ' + str(int(myage)+1)+1 + 'in a year' ) TypeError: Can't convert 'int' object to str implicitly – Punith Kothari Aug 19 '17 at 07:01
  • print('you will be ' + str(int(myage) + 1) + '1 + in a year' ) – Punith Kothari Aug 19 '17 at 07:05
  • this will print me 271 not 27 :( – Punith Kothari Aug 19 '17 at 07:05
  • that was my old answer. remember to refresh the page and try my edited answer @PunithKothari :) – J.Doe Aug 19 '17 at 07:06
  • thank you so much J.Doe .It helped me .It will be great if you can help me understand where to us the '+' operators as in this case i was not sure how to use it. – Punith Kothari Aug 19 '17 at 07:09
  • @PunithKothari sure I am so glad to help :) I will type up an explanation. If it worked please accept my answer. check that little green check mark – J.Doe Aug 19 '17 at 07:10
  • @PunirthKothari usually if you are printing more than one argument in your `print()` statement you want to join them using `+`. and when you join arguments together remember their `type` have to be the same. – J.Doe Aug 19 '17 at 07:15
  • Just to note, this will be less confusing @PunithKothari if you used `print('you will be', int(myage) + 1, 'in a year')`which'll put the spaces in for you, make it more clear that you're converting myage to an int and adding one, and avoid the whole concatenating the string together yourself. – Jon Clements Aug 19 '17 at 07:48
1

You got an extra 1. change print('you will be ' + str(int(myage) + 1) + 1 'in a year') to

print('you will be ' + str(int(myage) + 1) + ' in a year')

0

Check your code here

Try :

# this program says hello and ask my name.
print('Hello World')
print('Whats your name?')
myname = input()
print('its good to meet you, ' + myname) #ask for my name
print('the length of your name is : ')
print(len(myname))
print('whats your age?')#ask for my age
myage = input()
print('you  will be ' + str(int(myage) + 1) + ' in a year')
Md. Rezwanul Haque
  • 2,882
  • 7
  • 28
  • 45
0

Following will solve your problem:

print('Hello World')
print('Whats your name?')
myname = raw_input()
print('its good to meet you, ' + myname) #ask for my name
print('the length of your name is : ')
print(len(myname))
print('whats your age?')#ask for my age
myage = raw_input()
print("you  will be " + str(int(myage) + 1) + " years old by next year")

You have missed out the '+' in your line 10 to concatenate your strings. Also there was an unwanted '+ 1' which would have caused another error for you as you were trying to concatenate int and str objects.

Another correction, I have changed the input() function with raw_input(), as I am using python2. If you need any better explanation on this chnage, you could check this link.

HardCoder
  • 249
  • 3
  • 12
0

you should have a string in this command:

print('you  will be ' + str(int(myage) + 1) + 1 'in a year')

these are your strings: 'you will be ' and str(int(myage) + 1) and 'in a year'. but you are wrong in writing 1 'in a year'. this is not string. i think you don't need to the second "1". you can write:

print('you  will be ' + str(int(myage) + 1) +'in a year')
Mahsa
  • 581
  • 1
  • 9
  • 28