Python Type Error[SOLVED]

Python Type Error[SOLVED]

Postby rhyswilson92 » Wed Mar 13, 2013 5:59 pm

Having an issue with a little python cgi programming, I run this and I keep getting this error :

Content-Type: text/html
<html>
<head><title>CGI Program</title></head>
<body>
Traceback (most recent call last):
File "./hello_get.py", line 18, in <module>
print "<h2>Hello &s &s</h2>" % first_name, last_name
TypeError: not all arguments converted during string formatting

This is my code, any help would be much appreciated.
Code: Select all
#!/usr/bin/python

# Import modules for CGI handling
import cgi, cgitb

# Create instance of FieldStorage
form = cgi.FieldStorage()

# Get data from fields
first_name = form.getvalue('first_name')
last_name = form.getvalue('last_name')


print "Content-Type: text/html"
print "<html>"
print "<head><title>CGI Program</title></head>"
print "<body>"
print "<h2>Hello &s &s</h2>" % first_name, last_name
print "</body>"
print "</html>"
Last edited by rhyswilson92 on Thu Mar 14, 2013 2:26 pm, edited 1 time in total.
rhyswilson92
 
Posts: 5
Joined: Tue Mar 12, 2013 8:47 pm

Re: Python Type Error,

Postby metulburr » Wed Mar 13, 2013 6:10 pm

1) You have ampersands instead of percent
2) no parenthesis
Code: Select all
first_name = 'micah'
last_name = 'page'

print "<h2>Hello %s %s</h2>" %(first_name, last_name)


try the newer str.format()
Code: Select all
first_name = 'micah'
last_name = 'page'

print "<h2>Hello {} {}</h2>".format(first_name, last_name)
New Users, Read This
OS Ubuntu 14.04, Arch Linux, Gentoo, Windows 7/8
https://github.com/metulburr
steam
User avatar
metulburr
 
Posts: 1469
Joined: Thu Feb 07, 2013 4:47 pm
Location: Elmira, NY

Re: Python Type Error[SOLVED]

Postby rhyswilson92 » Thu Mar 14, 2013 2:26 pm

Hi metulburr

Thanks for your reply that did help, I don't know how I never spotted the mistake.

Thanks
rhyswilson92
 
Posts: 5
Joined: Tue Mar 12, 2013 8:47 pm


Return to Web Development

Who is online

Users browsing this forum: No registered users and 2 guests