#!C:\Python26\python.exe

# Three-times-the-square-root-of-your-age.py
#
# This is a python program that
# will input the user's name and age
# the output will be 3 times the
# square root of the user's name
#
# author: Min Ku "Peter" Joo
# last revision: 02/10/2010

import os
import math

def main ( ):
    sayWelcome ( )
    getUsersName ( )
    getUsersAge ( )
    calThreeSqrtOfAge ( )
    tellUserOutcome ( )
    programPause ( )

def sayWelcome ( ):
    # Welcomes the user to the program
    print "Welcome to Peter's program."

def getUsersName ( ):
    # asks the user for his or her full name
    usersFullName = raw_input ("What is your full name? " )
    return usersFullName

def getUsersAge ( ):
    # asks the user for his or her age
    usersAge = raw_input ("What is your age in whole numbers? ")
    return usersAge

def calThreeSqrtOfAge ( ):
    #calculates three times the square root of user's age
    threeSqrtAge = 3*math.sqrt(usersAge)
    return threeSqrtAge

def tellUserOutcome ( ):
    # informs user the outcome of 3 times the square root of his or her age
    print usersFullName, '3 times the square root of your age is:', threeSqrtAge 

def programPause ( ):
    raw_input ("This is the end of Peter's program. Press enter to exit the program.")


if __name__ == '__main__':
    main()

how do i get assign variables to be used in other functions?
for example the age that the user inputs. how do i use that in both the function to get the age and the function to calculate 3*math.sqrt (age)

Recommended Answers

All 2 Replies

In your main function when you call getUserAge() assign the return to a variable its self. If you want to use it in another function the you will need to have the function you want to use it in have a input parameter.

userAge = getUserAge()
def calThreeSqrtOfAge (userAge)

You have to pass the required arguments to an from your functions. Look at your modified code carefully ...

#!C:\Python26\python.exe

# Three-times-the-square-root-of-your-age.py
#
# This is a python program that
# will input the user's name and age
# the output will be 3 times the
# square root of the user's name
#
# author: Min Ku "Peter" Joo
# last revision: 02/10/2010 modified

import os
import math

def main ( ):
    sayWelcome ( )
    usersFullName = getUsersName ( )
    usersAge = getUsersAge ( )
    threeSqrtAge = calThreeSqrtOfAge ( usersAge )
    tellUserOutcome ( usersFullName, threeSqrtAge )
    programPause ( )

def sayWelcome ( ):
    # Welcomes the user to the program
    print "Welcome to Peter's program."

def getUsersName ( ):
    # asks the user for his or her full name
    usersFullName = raw_input ("What is your full name? " )
    return usersFullName

def getUsersAge ( ):
    # asks the user for his or her age
    usersAge = raw_input ("What is your age in whole numbers? ")
    return int(usersAge)

def calThreeSqrtOfAge ( usersAge ):
    #calculates three times the square root of user's age
    threeSqrtAge = 3*math.sqrt(usersAge)
    return threeSqrtAge

def tellUserOutcome ( usersFullName, threeSqrtAge ):
    # informs user the outcome of 3 times the square root of his or her age
    print usersFullName, '3 times the square root of your age is:', threeSqrtAge 

def programPause ( ):
    raw_input ("This is the end of Peter's program. Press enter to exit the program.")


if __name__ == '__main__':
    main()
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.