-2

For an assignment i am trying to get the results of a form "age" and to add one to that number using python. The form will have users enter their age and result should be their age next year.

Here is what i have thus far:

import cgi
form = cgi.FieldStorage()

name = str(form.getvalue("name"))
age = int(form.getvalue("age"))

print ("""Content-type: text/html

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html><head>
<title>Lab 9</title>
</head><body>
""")

print ("<p>Hello," +name+ ".</p>")
print ("Next year you will be" + str(age+1) + "years old")
print ("</body></html>") 

The error i get is

Traceback (most recent call last):
    File "/Users/JLau/Documents/CMPT 165/Lab 9/result.py", line 19, in <module>
        print ("Next year you will be" + str(age + 1) + "years old")
    TypeError: Can't convert 'int' object to str implicitly"

I somehow need to convert the value of age to and "int" to which a number can be added, not sure how to do this.

pfnuesel
  • 14,093
  • 14
  • 58
  • 71
Justin Lau
  • 27
  • 3
  • Is this your complete code? Seems like somewhere in your code age is being cast to string and issue is with adding 1 to string value of age. – Ankit Jaiswal Jul 25 '14 at 07:55
  • Try print ("Next year you will be ", age+1, " years old") – Stephen Lin Jul 25 '14 at 07:55
  • There is an html document formatted with a text box where they can enter their age and the python program is meant to run the calculation – Justin Lau Jul 25 '14 at 08:03
  • possible duplicate of [TypeError: Can't convert 'int' object to str implicitly](http://stackoverflow.com/questions/13654168/typeerror-cant-convert-int-object-to-str-implicitly) – pfnuesel Jul 25 '14 at 08:05
  • Voting to close as not reproducible. Please read [mre]. – Karl Knechtel Jul 27 '22 at 03:49

4 Answers4

1

That can't be the code you're running.

Check the following test.

>>> age = 20
>>> print ("Next year you will be" + str(age+1) + "years old")
Next year you will be21years old
>>> age = '20'
>>> print ("Next year you will be" + str(age+1) + "years old")
Traceback (most recent call last):
  File "<pyshell#4>", line 1, in <module>
    print ("Next year you will be" + str(age+1) + "years old")
TypeError: Can't convert 'int' object to str implicitly

You'll get this error if age is a string.

Please add a line with

print(type(age))

and look at the result.

Matthias
  • 12,873
  • 6
  • 42
  • 48
  • If I do that, I get `TypeError: cannot concatenate 'str' and 'int' objects`. How come you get a different error message? – pfnuesel Jul 25 '14 at 08:07
  • Yea it looks like it is a STR, how can i get that to an INT so it can be added correctly? – Justin Lau Jul 25 '14 at 08:09
  • 1
    I use Python 3.3. Did you add the line with the print before ther erroneous line? – Matthias Jul 25 '14 at 08:09
  • The line `age = int(form.getvalue("age"))` should do that for you. Is there some other code that changes `age` back to be a string? – Matthias Jul 25 '14 at 08:10
  • 1
    You might just use `str(int(age) + 1)` but it would be better to find the reason why `age` is a string. – Matthias Jul 25 '14 at 08:11
  • I tried that as well, got an error saying "TypeError: int() argument must be a string or a number, not 'NoneType'" Thanks for help by the way! – Justin Lau Jul 25 '14 at 08:12
  • A `None` type! So somewhere `age` is set to `None`. It can't happen when you do the first type cast because `int(None)` would lead to `TypeError: int() argument must be a string or a number, not 'NoneType'`. – Matthias Jul 25 '14 at 08:15
  • 1
    Show us your complete code. Alternatively: Try the code you posted in the question. It should work. Now add bit for bit the remaining parts of your code and see where it breaks. There's your flaw. – pfnuesel Jul 25 '14 at 08:16
0

You can't add ints when you make them str type using str().

print("Next year you will be",age+1,"years old")

is the code you are looking for.

Update: Another possible reason for your error is your use of cgi - according to Python documentation cgi often throws up errors when being tested. You should follow the instructions on the link to debug it.

However, the problem might lie in the line:

age = int(form.getvalue("age"))

because int() won't convert NoneTypes to an int type, hence the error.

TheDarkTurtle
  • 413
  • 1
  • 3
  • 12
0

In fact, it is better to use string templates, as in:

   print("Next year you will be %s year old" % (age + 1, ))
manuBriot
  • 2,755
  • 13
  • 21
0

You cannot concatenate a string with an int. You would need to convert your int to string using str function, or use formatting to format your output.

So the problem lies in str(age + 1). so remove str from it and be left with age + 1

update:

I just did the following in IDLE and had no problem

>>> age = 5
>>> age + 1
    6
>>> print ("hello, I'm", age + 1, "years old")
("hello, I'm", 6, 'years old')
>>> 
user958119
  • 63
  • 6
  • If i remove STR i simply get none + 1 as the result. it doesn't actually add the two together, rather it attaches "1" at the end of whatever the input – Justin Lau Jul 25 '14 at 08:02
  • what is plain wrong? I just did the above update in IDLE and didn't see any problem so instead of stating plain wrong why don't u show the plain wrong you're talking about – user958119 Jul 25 '14 at 08:04
  • 1
    Now you use a `,`. The code in the question concatenates Strings with `+`. You didn't get the reason for the wrong behaviour. – Matthias Jul 25 '14 at 08:08
  • oh ok. I see. it doesn't add the 1, then you should perhaps use string substitution or do that outside the addition print statement like I did. – user958119 Jul 25 '14 at 08:08