Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here


Newbie Python (CGI) question

Newbie Python (CGI) question

Newbie Python (CGI) question

Hi. I'm new to Python, trying to teach myself. I have the following script which works fine but I'm wondering if there are better ways of doing some things.



import cgi, cgitb

form = cgi.FieldStorage()

# Get data from form fields
# text fields
tname = form.getvalue('tname')
tdate = form.getvalue('tdate')

# textarea
if form.getvalue('comments'):
   comments = form.getvalue('comments')
   comments = "None"

# checkbox
if form.getvalue('certificate'):
   checkbox1 = "yes"
   checkbox1 = "no"

if form.getvalue('alumni'):
   checkbox2 = "yes"
   checkbox2 = "no"

# radio button
if form.getvalue('grade'):
   grade = form.getvalue('grade')
   grade = "Not set"

# dropdown
if form.getvalue('subject'):
   subject = form.getvalue('subject')
   subject = "Not entered"

aoran = "a "
if (grade == "ordinary") or (grade == "upper second"):
   aoran = "an "

if ( grade != "ordinary" ):
   grade = grade + ' class honours'

print("""<!doctype html>
<html lang="en-gb">
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
body {
  font: .92em Verdana, Arial, Helvetica, sans-serif;
  text-align: center;
div {
  width: 20em;
  margin: 2em auto;
  padding: 2em;
  border: 1px solid black;
print("<h2>This is to certify that</h2>")
print("<h3> %s </h3>" % (tname))
print("<p>has been awarded</p>")
print("<p> %s %s degree</p>" % (aoran, grade))
print("<p>in %s </p>" % (subject))
print("<p>Date: %s </p>" % (tdate))
print("<p>Comments: %s </p>" % (comments))
print("<p>--- for official use ---<br>certificate: %s; alumni: %s </p>" % (checkbox1, checkbox2))

The page that calls this script is at http://gandalf458.co.uk/certificate.html if that helps.


I'm not a number, I'm a free man

RE: Newbie Python (CGI) question


I see nobody more qualified answered this, so I will enumerate some minor things I would write differently. Absolutely no intention to call my code better, is just a more compact coding style I find more readable.

CODE --> Python

if form.getvalue('comments'):
   comments = form.getvalue('comments')
   comments = "None"

#   │
#   ⋁

comments = form.getvalue('comments', 'None') 

CODE --> Python

if form.getvalue('certificate'):
   checkbox1 = "yes"
   checkbox1 = "no"

#   │
#   ⋁

checkbox1 = ['yes', 'no'][not form.getvalue('certificate')] 

CODE --> Python

aoran = "a "
if (grade == "ordinary") or (grade == "upper second"):
   aoran = "an "

#   │
#   ⋁

aoran = ['a', 'an'][grade[0] in 'aeiou'] 


RE: Newbie Python (CGI) question

Hi Feheke

That's just the kind of feedback I was hoping for. Many thanks

I'm not a number, I'm a free man

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close