Challenger88
Challenger88

Reputation: 13

Python - Use one variable from a function in other functions

I'm new to programming and I'm working on a program that I want to use variables from another function. I understand you can't use local variables from another function, so I added a new variable in the main function to create a copy of the variable for the next function to use. The problem is that when I use BirthYear = get_Input() in the main function, it repeats the get_Input function aswell, creating 2 input prompts. Is there any way to return a variable and pull it into the next function without having to repeat my input function? I have read many articles on StackOverflow that say to use classes(I don't want to use classes) and to call the input function within my Calculations function, but I still get prompted for 2 copies of input there too.

I am also getting this error when trying to pass a variable to a function from the main. TypeError: get_Calculations() missing 1 required positional argument: 'BirthYear'

Here is my Code:

# Declare variables: name, data type and purpose
MinYear = 0 # integer, start year in zodiac table
MaxYear = 0 # integer, final year in zodiac table
BirthYear = 0 # integer, year of birth entered by user
Index = 0 # integer, calculated position (column) in zodiac table
Prompt = "" # string, used to build input prompts
Sign = "" # string, calculated zodiac sign
AnimalsOfZodiac = [""] # list of zodiac signs
Result = "" # string, holds output

# list of zodiac signs in chronological order
AnimalsOfZodiac = ["Rat","Ox","Tiger","Rabbit","Dragon","Snake","Horse","Sheep","Monkey","Rooster","Dog","Pig"]

# Display name of program, any instructions
def get_Instructions():
    print("Chinese Zodiac Calculator")
    print("Find the Chinese zodiac sign for birth years between 1924 and 2031\n")

# Input: get values
MinYear = 1924
MaxYear = 2031

def get_Input():
    Prompt = "Enter a birth year between " + str(MinYear) + " and " +\
              str(MaxYear) + ": "
    BirthYear = int(input(Prompt))
    while BirthYear < MinYear or BirthYear > MaxYear:
        print("Invalid Birth year.")
        BirthYear = int(input(Prompt))

    return BirthYear

# Processing: carry out calculations
def get_Calculations(BirthYear):
    Index = (BirthYear - MinYear) % 12
    Sign = AnimalsOfZodiac[Index]

    return Sign

# Output: report result(s)
def get_Results(SignCopy, BirthYear):
    print("A person born in " + str(BirthYear) +\
    " was born under the sign of the " + Sign + ".")

def Main():

    get_Instructions()
    get_Input()
    BirthYear = get_Input()
    get_Calculations(BirthYear)
    SignCopy = get_Calculations()
    get_Results(SignCopy, BirthYear)

Main()

Upvotes: 0

Views: 227

Answers (3)

167141162
167141162

Reputation: 51

3 Simple changes to your code. comment out line's 48 and 50, and change the Sign variable in 43 to SignCopy.

Making your code:

# Declare variables: name, data type and purpose
MinYear = 0 # integer, start year in zodiac table
MaxYear = 0 # integer, final year in zodiac table
BirthYear = 0 # integer, year of birth entered by user
Index = 0 # integer, calculated position (column) in zodiac table
Prompt = "" # string, used to build input prompts
Sign = "" # string, calculated zodiac sign
AnimalsOfZodiac = [""] # list of zodiac signs
Result = "" # string, holds output
# list of zodiac signs in chronological order
AnimalsOfZodiac = ["Rat","Ox","Tiger","Rabbit","Dragon","Snake","Horse","Sheep","Monkey","Rooster","Dog","Pig"]
# Display name of program, any instructions
def get_Instructions():
    print("Chinese Zodiac Calculator")
    print("Find the Chinese zodiac sign for birth years between 1924 and 2031\n")
# Input: get values
MinYear = 1924
MaxYear = 2031
def get_Input():
    Prompt = "Enter a birth year between " + str(MinYear) + " and " +\
              str(MaxYear) + ": "
    BirthYear = int(input(Prompt))
    while BirthYear < MinYear or BirthYear > MaxYear:
        print("Invalid Birth year.")
        BirthYear = int(input(Prompt))
    return BirthYear
# Processing: carry out calculations
def get_Calculations(BirthYear):
    Index = (BirthYear - MinYear) % 12
    Sign = AnimalsOfZodiac[Index]
    return Sign
# Output: report result(s)
def get_Results(SignCopy, BirthYear):
    print("A person born in " + str(BirthYear) +\
" was born under the sign of the " + SignCopy + ".")
def Main():
    get_Instructions()
    #get_Input()
    BirthYear = get_Input()
    #get_Calculations(BirthYear)
    SignCopy = get_Calculations(BirthYear)
    get_Results(SignCopy, BirthYear)
Main()

Upvotes: 1

Reblochon Masque
Reblochon Masque

Reputation: 36662

If you assign the return value of get_Input to a variable named BirthYear in main(), you do not need to call get_Input() twice.

You then need to pass BirthYear to get_Calculations()

def Main():

    get_Instructions()
    BirthYear = get_Input()
    get_Calculations(BirthYear)
    SignCopy = get_Calculations(BirthYear)
    get_Results(SignCopy, BirthYear)

Upvotes: 0

Burhan Khalid
Burhan Khalid

Reputation: 174614

Your problem is that you are calling the same function twice, for some reason.

def Main():

    get_Instructions()
    # get_Input() - no need for this
    BirthYear = get_Input()
    SignCopy = get_Calculations(BirthYear)
    # SignCopy = get_Calculations() - no need for this
    get_Results(SignCopy, BirthYear)

Upvotes: 0

Related Questions