#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2005
    Posts
    49
    Rep Power
    0

    Form handling - Python Script


    Hi! ,all

    I am new to python programming,

    I have developed a HTML Form

    with two fields to perform Addition of two numbers.

    in my python code

    i get the value of two fields by


    form = cgi.FieldStorage()
    if form.has_key("input1"):
    val1 = cgi.escape(form["input1"].value)
    if form.has_key("input2"):
    val2 = cgi.escape(form["input2"].value)
    val3=val1+val2
    print "<h1>Hello",val1+val2, "</h1>"

    to perform addition

    If the form input is 2 and 4 the output should be 6

    but my output is 24 , it does concatenation.

    how can i handle this, let me know.

    Arpan
  2. #2
  3. Banned ;)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Location
    Woodland Hills, Los Angeles County, California, USA
    Posts
    9,592
    Rep Power
    4207
    Convert both var1 and var2 to int, before doing the addition. One place you can do it is here:
    var3 = var1 + var2
    You can instead do this:
    var3 = int(var1) + int(var2)

    That should do it for you.
    Up the Irons
    What Would Jimi Do? Smash amps. Burn guitar. Take the groupies home.
    "Death Before Dishonour, my Friends!!" - Bruce D ickinson, Iron Maiden Aug 20, 2005 @ OzzFest
    Down with Sharon Osbourne

    "I wouldn't hire a butcher to fix my car. I also wouldn't hire a marketing firm to build my website." - Nilpo
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2005
    Posts
    49
    Rep Power
    0
    Thank you, it worked,

    One more doubt,

    Wat if i want to design a calculator, using the same strategy

    i will have four buttons ADD SUB MUL and DIV

    in my code, i have to put something like

    if buttonpresed=ADD
    then val1+val2
    else if buttonpress=MUL
    then val1*val2

    How can i implement this feature using python,

    so my code recoznizes which button i am pressing.

    Let me know,

    I would appreicate it,

    thanks
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2005
    Posts
    49
    Rep Power
    0
    Thank you, it worked,

    One more doubt,

    Wat if i want to design a calculator, using the same strategy

    i will have four buttons ADD SUB MUL and DIV

    in my code, i have to put something like

    if buttonpresed=ADD
    then val1+val2
    else if buttonpress=MUL
    then val1*val2

    How can i implement this feature using python,

    so my code recoznizes which button i am pressing.

    Let me know,

    I would appreicate it,

    thanks

IMN logo majestic logo threadwatch logo seochat tools logo