Accessing returned values from a function, by another function

前端 未结 2 869
孤街浪徒
孤街浪徒 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: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)
    

提交回复
热议问题