Jump to content

Determining next year's age

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
4 replies to this topic

#1
taabistan

taabistan

    Newbie

  • Members
  • Pip
  • 7 posts
This is the link to my website:

Name and Age

I want someone to write their name down and then tell me their age. This is the result I want:

Hello {insert name}

Next year, you will be {insert age} years old

Basically if I told the web-site my name is Brian and I'm 15 years old, it should say 16 years old.

Here is the xHTML file:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html>

<head>

<title>Day of the Week</title>

</head>

<body>


<h1>Day of the Week</h1>


<form action="weekday.py">

<p>

Enter the date (yyyy, mm, dd): 

<input type="text" name="year" size="4" maxlength="4" />

<input type="text" name="month" size="2" maxlength="2" />

<input type="text" name="day" size="2" maxlength="2" />


</p>

<p>

<input type="submit" value="Give me the weekday" />

</p>


</form>


</body>

</html>


The python code:

#!/Python26/python

import calendar, cgi

import cgitb; cgitb.enable()


form = cgi.FieldStorage()


day = form.getvalue('day')

if day:

    day = int(day)

month = form.getvalue('month')

if month:

    month = int(month)

year = form.getvalue('year')

if year:

    year = int(year)

weekday = form.getvalue('weekday')

if weekday:

    weekday = int('weekday')



def getDay(day):

    if  (weekday == 0):  

        day = "Monday"

    elif (weekday == 1):

        day = "Tuesday"

    elif (weekday == 2):

        day = "Wednesday"

    elif (weekday == 3):

        day = "Thursday"

    elif (weekday == 4):

        day = "Friday"

    elif (weekday == 5):

        day = "Saturday"

    else:

        day = "Sunday"

    return day



print "Content-Type: text/html\n\n"

print "<head>"

print "<title>Weekday</title>"

print "</head>"

print "<body>"

print "<p>That's a %s</p>" % (getDay(weekday))

print "</body>" 



#2
Flying Dutchman

Flying Dutchman

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 890 posts
You posted wrong code. Also, I got an error after I've submitted:
Content-type: text/html

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html><head>
<title>A CGI Script</title>
</head><body>

<p>Hello A.</p>
Traceback (most recent call last):
  File "/home/jnoori/public_html/age.py", line 21, in <module>
    print "<p>Next year, you will be " + text2 + " years old.</p>"
TypeError: cannot concatenate 'str' and 'int' objects

A conclusion is where you got tired of thinking.
#define class struct    // All is public.

#3
taabistan

taabistan

    Newbie

  • Members
  • Pip
  • 7 posts
That's why I'm seeking advice.

#4
Vladimir

Vladimir

    Learning Programmer

  • Members
  • PipPipPip
  • 79 posts
How do you run this code? May be I miss something but you can not execute this without Http bindings...

Python does not automatically convert types for you. You have manually convert int to str to be able to concatenate strings:

print "<p>Next year, you will be " + str(text2) + " years old.</p>" # str(text2) - convert int to str

But much better way is using 5. Built-in Types — Python v2.7.1 documentation

print "<p>Next year, you will be %d years old.</p>" % text2 # str(text2) - convert int to str

You can rewrite this:

def getDay(day):

    if  (weekday == 0):  

        day = "Monday"

    elif (weekday == 1):

        day = "Tuesday"

    elif (weekday == 2):

        day = "Wednesday"

    elif (weekday == 3):

        day = "Thursday"

    elif (weekday == 4):

        day = "Friday"

    elif (weekday == 5):

        day = "Saturday"

    else:

        day = "Sunday"

    return day

like this:

def get_day_name(num):

    days = {1: 'Monday',

                # fill missing

                7: 'Sunday'}

    return days[num]



#5
_Khalil_

_Khalil_

    Newbie

  • Members
  • Pip
  • 9 posts
I am new to programming with python so I won't be much of a help.

Although I do suggest you have them enter the year they were born and then compare it to the current year. Then have it calculate the age of the person or how many years are between their birth year and the current year.