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

2024-10-01 17:33:45 发布

您现在位置:Python中文网/ 问答频道 /正文

我是编程新手,我正在编写一个程序,我想使用另一个函数的变量。我知道不能使用另一个函数的局部变量,所以我在main函数中添加了一个新变量,以创建该变量的副本,供下一个函数使用。问题是,当我在main函数中使用BirthYear = get_Input()时,它也会重复get\u Input函数,创建两个输入提示。有没有什么方法可以返回一个变量并将其拉入下一个函数,而不必重复输入函数?我读过很多关于StackOverflow的文章,上面说要使用类(我不想使用类)并在我的Calculations函数中调用input函数,但是我仍然得到提示,在那里输入了2个副本。你知道吗

我在尝试从main向函数传递变量时也遇到了这个错误。TypeError:get\u 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()

Tags: of函数ininputgetdefintegeryear
3条回答

如果将返回值get_Input赋给main()中名为BirthYear的变量,则无需调用get_Input()两次。你知道吗

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

def Main():

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

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

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)

对代码的3个简单更改。 注释掉第48行和第50行,并将第43行中的符号变量更改为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()

相关问题 更多 >

    热门问题