0

What's wrong with the line6:

 name = (input('Hi, please enter your name '))
dob = int(input('Hi please enter your DOB '))
today = int(input("Hi please enter today's date "))
future = int(today + 5)
diff = str(future - dob)
print ('Hi ' + name + 'in the year of ' + future + 'you will turn ' + diff)

I keep getting error:

TypeError: must be str, not int

How do you guys debug in general? I'm new to programming. Is there a way to know exactly where it wants string?

khelwood
  • 55,782
  • 14
  • 81
  • 108
Galapagos
  • 669
  • 6
  • 13
  • You should be able to see the line number where the error occurred in the traceback. In general, you can debug by removing things until the error disappears, then put the last thing you removed back (in which you will find the error). – L3viathan Nov 05 '18 at 18:18

2 Answers2

2

Python can't automatically convert integer variable to string.

You need to explicitly convert future variable to str type like this:

print ('Hi ' + name + 'in the year of ' + str(future) + 'you will turn ' + diff)
Dmitriy Fialkovskiy
  • 3,065
  • 8
  • 32
  • 47
1

future is an integer, therefore, it cannot be concatenated with a string. You can do this:

print ('Hi ' + name + 'in the year of ', future, 'you will turn ' + diff)

OR cast int to str

print ('Hi ' + name + 'in the year of ' + str(future) + 'you will turn ' + diff)

Example:

name = (input('Hi, please enter your name '))
dob = int(input('Hi please enter your DOB '))
today = int(input("Hi please enter today's date "))
future = int(today + 5)
diff = str(future - dob)
print ('Hi ' + name + 'in the year of ', future, 'you will turn ' + diff)

Output:

C:\Users\Documents>py test.py
Hi, please enter your name job
Hi please enter your DOB 1876
Hi please enter today's date 2018
Hi jobin the year of  2023 you will turn 147

Example:

name = (input('Hi, please enter your name '))
dob = int(input('Hi please enter your DOB '))
today = int(input("Hi please enter today's date "))
future = int(today + 5)
diff = str(future - dob)
print ('Hi ' + name + 'in the year of ' + str(future) + 'you will turn ' + diff)

Output:

Hi, please enter your name job
Hi please enter your DOB 1876
Hi please enter today's date 2018
Hi jobin the year of 2023you will turn 147
Rarblack
  • 4,559
  • 4
  • 22
  • 33
  • your solution doesn't seem to work: – Galapagos Nov 05 '18 at 19:38
  • Why? It is working – Rarblack Nov 05 '18 at 19:41
  • Look both are perfectly working @Galapagos. – Rarblack Nov 05 '18 at 19:45
  • the original suggested: future = str(int(today + 5)) print ('Hi ' + name + 'in the year of ' + future + 'you will turn ' + diff) so inserting into the code: name = (input('Hi, please enter your name ')) dob = int(input('Hi please enter your DOB ')) today = int(input("Hi please enter today's date ")) future = str(int(today + 5)) diff = str(future - dob) print ('Hi ' + name + 'in the year of ' + future + 'you will turn ' + diff) does not work . – Galapagos Nov 05 '18 at 19:45
  • I know what is your point? – Rarblack Nov 05 '18 at 19:46