My code is the following:

import cgi
data = cgi.FieldStorage()

productdescription = data.getvalue('product_description')
listprice = data.getvalue('list_price')
discountpercent = data.getvalue('discount_percent')

def calc(discountpercent):
    if discountpercent > 100:
        raise ValueError("Percentage discount cannot exceed 100%")
    elif discountpercent < 100:
        percent = float(discountpercent) / 100
        discount = float(listprice) * percent
        return discount
        raise ValueError("Invalid discount type.")

total = listprice - discountpercent

print( 'Content-type:text/html\r\n\r\n' )
print( '<!DOCTYPE HTML>' )
print( '<html lang="en">' )
print( '<head>' )
print( '<meta charset="UTF-8">' )
print( '<title>Product Discount Calculator</title>' )
print( '</head>' )
print( '<body>' )
print( '<h1>Product Description:', product_description,'</h1>' )
print( '<h1>List Price:', list_price, '</h1>' )
print( '<h1>Discount:', discount_percent, '</h1>' )
print( '<h1>Subtotal:', total, '</h1>' )
print( '</body>' )
print( '</html>' )

The traceback error says:

Traceback (most recent call last):
  File "<string>", line 250, in run_nodebug
  File "C:\Abyss Web Server\htdocs\", line 18, in <module>
    total = listprice - discountpercent
TypeError: unsupported operand type(s) for -: 'NoneType' and 'NoneType'

I have no idea how to get this fixed, any help would be great.

You really shouldn't be be using CGI.
Did you read me answer in you other post?

You never call calc function,line 18 i think should be inside calc function.
To get the error you get both listprice and discountpercent most be None.

>>> listprice = 100
>>> discountpercent = 80
>>> total = listprice - discountpercent
>>> total
>>> listprice = None
>>> discountpercent = None
>>> total = listprice - discountpercent
Traceback (most recent call last):
  File "<interactive input>", line 1, in <module>
TypeError: unsupported operand type(s) for -: 'NoneType' and 'NoneType'

Edited 2 Years Ago by snippsat

I'm trying to get listprice from the value that the user enters on the html page, which is list_price.

This article has been dead for over six months. Start a new discussion instead.