Python help

I’m teaching myself Python and have created a simple HTML form with a Python back end. My form is

<!doctype html>
<html lang="en-GB">
<head>
<meta charset="utf-8">
<title>Certificate</title>
</head>
<body>
<h1>Form, innit!</h1>
<form action="converter.py" method="post">
  <fieldset>
  <legend>Please complete the following details:</legend>
  <label for="text">Name:</label>
  <input type="text" name="tname" id="tname">
  <br>
  <label for="subject">Subject:</label>
  <select name="subject" id="dropdown">
    <option value="Maths">Maths</option>
    <option value="Physics">Physics</option>
  </select>
  <br>
  <fieldset>
  <legend>Grade:</legend>
  <label for="grade1">1st</label>
  <input type="radio" name="grade" id="grade1" value="first">
  <label for="grade2">2:1</label>
  <input type="radio" name="grade" id="grade2" value="upper second">
  <label for="grade3">2:2</label>
  <input type="radio" name="grade" id="grade3" value="lower second">
  <label for="grade4">3rd</label>
  <input type="radio" name="grade" id="grade4" value="third">
  <label for="grade5">Pass</label>
  <input type="radio" name="grade" id="grade5" value="ordinary">
  </fieldset>
  <br>
  <fieldset>
  <legend>Complete:</legend>
  <label for="certificate">Certificate</label>
  <input type="checkbox" name="certificate" id="certificate" value="certificate">
  <label for="checkbox2">Alumni</label>
  <input type="checkbox" name="alumni" id="alumni" value="alumni">
  </fieldset>
  <br>
  <label for="textarea">Comments:</label>
  <textarea name="comments" id="comments"></textarea>
  </fieldset>
  <br>
  <input type="submit" value="Submit">
</form>
</body>
</html>

and my Python

print("Content-type:text/html\r\n")

# Import modules for CGI handling 
import cgi, cgitb 

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

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

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

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

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

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

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

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

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

print("""\
<!doctype html>
<html lang="en-gb">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Certificate</title>
</head>
<body>
<style>
body {
  font: .92em Verdana, Arial, Helvetica, sans-serif;
}
</style>""")
print("<h1>Certificate</h1>")
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>Comments: %s </p>" % (comments))
print("<p>certificate: %s </p>" % (checkbox1))
print("<p>alumni: %s </p>" % (checkbox2))
print('</body>')
print('</html>')

I have 2 questions, one specific and one general.

The specific one first. The lines

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

work fine if grade is ordinary but not if grade is upper second (in that I get “a upper second” rather than “an upper second”). Can someone tell me what I’m doing wrong here, please?

And the general question, are there better ways to do some of the things I’m doing here?

Thx

Okay, so I’m a pranny! I’ve solved the specific issue - got my code in the wrong order!

2 Likes

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.