Python:通过另一个函数访问函数的返回值

时间:2016-05-07 12:20:20

标签: python function return access

我对编程有点新手,刚刚开始真正进入python。 我正在开展一个猜测项目。

import random

def main(): # main function
    print("Welcome to the number guesser game")
    range_func()
    max_guess_number(lower_range_cut, upper_range_cut)
    evaluation(random_number, total_guesses)

def range_func():   # allows user to select a range for the number guess
    print("Please select a range in which you would like to guess.")
    lower_range_cut = int(input("Lower boundary limit: "))
    global lower_range_cut
    upper_range_cut = int(input("Upper boundary limit: "))
    global upper_range_cut
    random_number = random.randint(lower_range_cut,upper_range_cut)
    global random_number
    return lower_range_cut, upper_range_cut, random_number

def max_guess_number(low,high): # returns the total number of guesses
    total_numbers = (high - low) + 1
    total_guesses = 0
    while (2**total_guesses) < total_numbers:
        total_guesses += 1
    print ("You have a total of %d guesses\n"
           "for your range between %d to %d"
           % (total_guesses, low, high))
    global total_guesses
    return total_guesses

def evaluation(random_number, total_guesses): # evaluates the users input
    guess_count = 0
    while guess_count < total_guesses:
        user_guess = int(input("Your guess: "))
        print("Your guess is: %d" % (user_guess))
        if (random_number == user_guess):
            print("You got it ")
            break
        elif user_guess > random_number:
            print("Guess lower!")
            guess_count += 1
        else:
            print("Guess higher!")
            guess_count += 1

if __name__ == "__main__":
    main()

我写这篇文章时遇到的一个问题是,如果不将每个变量重新定义为全局变量,我就无法执行此程序。只是通过从一个函数返回值,我无法访问例如第二个从range_function

返回变量upper_range_cut

有没有办法处理那个更短的?

此外,我对代码本身的每个注释(可读性,功能使用,长度)感到高兴。我知道可能通过使用列表理解可以缩短这段代码,但我还没有真正关注这个领域的机会。

非常感谢您的帮助!

KiliBio

2 个答案:

答案 0 :(得分:2)

你几乎就在那里。您可以删除所有全局变量,然后将每个函数返回的值存储到局部变量,并将它们传递给新函数。

我在下面做的唯一其他改变是:

否则你看起来很好。

import random

def main(): # main function
    print("Welcome to the number guesser game")
    lower, upper, rand = range_func()
    total_guesses = max_guess_number(lower, upper)
    evaluation(rand, total_guesses)

def range_func():   # allows user to select a range for the number guess
    print("Please select a range in which you would like to guess.")
    lower_range_cut = int(input("Lower boundary limit: "))
    upper_range_cut = int(input("Upper boundary limit: "))
    random_number = random.randint(lower_range_cut, upper_range_cut)
    return lower_range_cut, upper_range_cut, random_number

def max_guess_number(low,high): # returns the total number of guesses
    total_numbers = (high - low) + 1
    total_guesses = 0
    while (2**total_guesses) < total_numbers:
        total_guesses += 1
    print ("You have a total of %d guesses\n"
           "for your range between %d to %d"
           % (total_guesses, low, high))

    return total_guesses

def evaluation(random_number, total_guesses): # evaluates the users input
    guess_count = 0
    while guess_count < total_guesses:
        guess_count += 1
        user_guess = int(input("Your guess: "))
        print("Your guess is: %d" % (user_guess))
        if (random_number == user_guess):
            print("You got it!")
            break
    else:
        print "Sorry, you didn't guess it in time. The answer was: %d" % random_number

if __name__ == '__main__':
    main()

答案 1 :(得分:1)

您无需定义global。您只需将从函数返回的值分配给变量即可。

一个简单的例子:

def add(a, b):
    """This function returns the sum of two numbers"""
    return a + b

现在在您的控制台中,您可以执行以下操作

# print the return
>>> print(add(2, 3))
5

# assign it to a variable
>>> c = add(2, 3)
>>> c
5

main函数中,您需要将不同函数返回的值分配给变量,这些变量可以进一步传递给其他函数。

def main(): # main function
    print("Welcome to the number guesser game")
    lower_range_cut, upper_range_cut, random_number = range_func()
    total_guesses = max_guess_number(lower_range_cut, upper_range_cut)
    evaluation(random_number, total_guesses)