Python - 在其他函数中使用函数中的一个变量

时间:2017-12-02 04:43:56

标签: python python-3.x

我是编程新手,我正在研究一个我想要使用其他函数变量的程序。我知道你不能使用来自另一个函数的局部变量,所以我在main函数中添加了一个新变量来为下一个要使用的函数创建变量的副本。问题是当我在main函数中使用BirthYear = get_Input()时,它会重复get_Input函数,并创建2个输入提示。有没有办法返回一个变量并将其拉入下一个函数而不必重复我的输入函数?我已经阅读了很多关于StackOverflow的文章,说要使用类(我不想使用类)并在我的计算函数中调用输入函数,但我仍然会在那里提示输入2个副本。

尝试将变量传递给main中的函数时,我也遇到此错误。 TypeError:get_Calculations()缺少1个必需的位置参数:' BirthYear'

这是我的代码:

# 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()

3 个答案:

答案 0 :(得分:1)

3代码的简单更改。 注释掉第48行和第50行,并将43中的Sign变量更改为SignCopy。

制作代码:

# 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()

答案 1 :(得分:0)

你的问题是你出于某种原因两次调用同一个函数。

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)

答案 2 :(得分:0)

如果您将get_Input的返回值分配给BirthYear中名为main()的变量,则无需再拨打get_Input()

然后,您需要将BirthYear传递给get_Calculations()

def Main():

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