Python Forum

Full Version: Help
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
My first code is:

def GetAge():
    print("How old are you? ")
    Years = float(input())
    return Years
    
def GetMonths(Years, Months):
    Months = Years * 12 
    return Months

def GetDays(Years, Days):
    Days = Years * 365 
    return Days
    
def GetHours(Years, Hours):
    Hours = Years * 365 * 24
    return Hours
    
def GetSeconds(Years, Seconds):
    Seconds = Years * 365 * 24 * 60 * 60
    return Seconds
    
def DisplaysResults(Months, Days, Hours, Seconds):
    print("This displays your age in months " + str(Months))
    print("This displays your age in days " + str(Days))
    print("This displays your age in hours " + str(Hours))
    print("This displays your age in seconds " + str(Seconds))
    
#Main

#This Program This program is divided into 3 modules/fuction(input, output and displays). The reason for this is to keep track of each step and not make it complicated.
Years = GetAge()
Months = GetMonths(Years, Months)
Days = GetDays(Years, Days)
Hours = GetHours(Years, Hours)
Seconds = GetSeconds(Years, Seconds)
print(Years, Months, Days, Hours, Seconds)
The error is :
Error:
Traceback (most recent call last):  File "python", line 32, in <module> NameError: name 'Months' is not defined
The other code is:


def GetMiles():
    print("How far are you from your distance in Miles? ")
    Miles = float(input())
    return Miles
 
def GetYards(Yards, Miles):
    Yards = Miles * 1760
    return Yards
def GetFeet(Feet, Miles):
    Feet = Miles * 5280
    return Feet
def GetInches(Inches, Miles):
    Inches = Miles * 63360
    return Inches
def DisplayResults(Yards, Feet, Inches):
    print("Your destination in miles convert to yards is this" + "=" + str(yards))
    print("Your destination in miles convert to feet is this" + "=" + str(feet))
    print("Your destination in miles convert to inches is this" + "=" + str(inches))
    print(" Drive safe" + " and " + "have a nice trip")
#Main

Miles = GetMiles()
Yards = GetYards(Yards, Miles)
Feet = GetFeet(Feet, Miles)
Inches = GetInches(Inches, Miles)
print(Yards, Feet, Inches)   
The error is:
Error:
Traceback (most recent call last):  File "python", line 23, in <module> NameError: name 'Yards' is not defined
It's pretty much the same error but different code, so can you help me?

Thank you!
Hi, First of all let's look at GetMonths function. You don't need to supply Months as argument. You just need Years and then return calculated Months.
Now the error - the error comes because at the time of the function call name Months does not exists.

def get_months(years):
    months = years * 12 
    return months
most people would simply:
def get_months(years):
    return years * 12
and call it (e.g. print the result) like this:

print(get_months(25))
as a general note - take a look at PEP-8 - the styling guide, naming conventions, etc.