Accessing returned values from a function, by another function

前端 未结 2 866
孤街浪徒
孤街浪徒 2020-12-04 01:59

I\'m kinda new to programming in general, just started to really get into python. And I\'m working on a number guesser project.

import random

def main(): #          


        
相关标签:
2条回答
  • 2020-12-04 02:15

    You're pretty much there. You can remove all globals, then just store the values returned from each function to local variables, and pass them in to new functions.

    The only other changes I've made below are:

    • Breaking out of the evaluation loop if the answer is guessed correctly.
    • Printing a message if no guess is found in the given time. See: Else clause on Python while statement
    • The bottom two lines allow the script to be run from the command line. See: What does if __name__ == "__main__": do?

    Otherwise you're looking good.

    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()
    
    0 讨论(0)
  • 2020-12-04 02:17

    You don't need to define global. You can just assign the values you are returning from a function to variable(s).

    A simple example:

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

    Now in your console, you could do following

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

    In your main function you need to assign the values which are returned by different functions to variables which you can further pass to other functions.

    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)
    
    0 讨论(0)
提交回复
热议问题