I am a student about 4 weeks into learning python, so i do not know a lot, and am hoping someone can help me understand what i am doing wrong, here is the code:

def main():
    name = "TEST"
    age = 50
    makename()
    makeage()
    print "You are" ,age ,"years old", name
def makename():
    name = raw_input("Please enter your name ")
    return name
def makeage():
    year = input("What is the year current year? ")
    byear = input("What year were you born in? ")
    age = year - byear
    return age
main()

inputs:
Please enter your name student_
What is the year current year? 2010
What year were you born in? 1985
output:
You are 50 years old TEST

what have i done incorrectly?

Recommended Answers

All 9 Replies

Like this,i have put function call in variable name and age.

def main():    
    name = makename()
    age = makeage()    
    print "You are" ,age ,"years old", name
def makename():
    name = raw_input("Please enter your name ")
    return name
def makeage():
    year = input("What is the year current year? ")
    byear = input("What year were you born in? ")
    age = year - byear
    return age
main()

snippsat has it right. You can, however, manipulate global variables from inside a function if you declare them global, though for most purposes this is not a good idea. For instance

def makeage():
  global age
  # set age = some calculation here
  # and the global age is altered

You have a couple of other issues: input and raw_input return strings. You cannot do subtraction on strings, so you will need to cast the string to an int.

You don't need to ask for the year: You can get it from the time module:

import time
year = time.localtime(time.time()).tm_year

http://docs.python.org/library/time.html has details.

Thank you for the quick response! When i made that change it worked perfectly. Not sure if i should make a new thread for this, but how could i use import to assign a variable value? here is an example:

makeName.py-

def makeName():
    name = raw_input("Please enter your name")
    return name
makeName()

makeAge.py-

def makeAge():
    year = input("What is the year current year? ")
    byear = input("What year were you born in? ")
    age = year - byear
    return age 
makeAge()

nameAge/py-

def makeAge():
    year = input("What is the year current year? ")
    byear = input("What year were you born in? ")
    age = year - byear 
    return age
makeAge()
def main():
    name = import makename
    age = import makeage
main()

The import function is not covered in the class i am taking since we are only learning python to learn how to program. thanks in advance if you take the time to look this over and offer any suggestions I am VERY new to programming

Why do you need separate modules? Its small enough not to really matter...
Python is case-sensitive so when you import something, it must follow the same case
You use import at the beginning.

from makeAge import makeAge 
from makeName import makeName
def main():
    name = makeAge()
    age = makeAge()
main()

Thanks griswolf, i checked out that link and must admit, that is all a bit over my head right now, but i am sure that site alone will give me a foot up on the rest of my class!

redyugi - i am taking a class to learn what programming, every program i make is required to be in that format, no matter how big or small. my goal here is to be able to make a module, and import it when needed, from looking at your code could i a file commonFuctions.py and store makeName() and makeAge() in in and call them like this:

from commonModules import makeAge 
from commonModules import makeName
def main():
    name = makeAge()
    age = makeAge()
main()

You can accomplish your module task by first creating the file commonModules.py

# save as commonModules.py

def makeName():
    name = raw_input("Please enter your name: ")
    return name

def makeAge():
    year = input("What is the current year? ")
    byear = input("What year were you born in? ")
    age = year - byear
    return age

# test the module
if __name__ == '__main__':
    name = makeName()
    print(name)
    age = makeAge()
    print(age)

Then create the program that uses the above module ...

# commonModules_test.py
# to make it simple have commonModules.py in the same folder

from commonModules import makeAge, makeName

def main():
    name = makeName()
    age = makeAge()
    print name, "you are", age, "years old"

main()

A little more on importing.
First the code.

#make_name.py
#saved in python26 folder
def makeName():
    name = raw_input("Please enter your name")
    return name

So how to use it.

>>> import make_name
>>> dir(make_name)
['__builtins__', '__doc__', '__file__', '__name__', '__package__', 'makeName']
>>> #We look at spesial methods 2 underscore and makName function
>>> make_name.__file__
'C:\\Python26\\make_name.py'
>>> make_name.__name__
'make_name'
>>> make_name.__doc__
>>> #We have not made a doc string in this function
 
>>> make_name.makeName
<function makeName at 0x01DCFCB0>
>>> #We se function makeName and a memory location
>>> make_name.makeName()
Please enter your nameTom
'Tom'
>>> #Call the function,remember we dont need a print statement when we do this in IDLE.

>>> from make_name import makeName
>>> makeName()
#Now i have put make_name in global namespace and can call only function name

awesome! thank you guys so much, this is going to make my life so much easier!
@vegaseat thanks for spelling it out for me, that really helped!
@snippsat i am going to got try that out, not sure i fully understand what is going on there, but i'm gonna try and see what i learn.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.