Python return语句的作用是什么?

Python return语句的作用是什么?,python,Python,我试图创建一个计算生日悖论的程序: import random def random_birthdays(): count = 0 res = [] for i in range(23): res.append(random.randint(0, 365)) res.sort() return res def final(): count = 0 i = 0 random_birthdays() for

我试图创建一个计算生日悖论的程序:

import random

def random_birthdays():
    count = 0
    res = []
    for i in range(23):
        res.append(random.randint(0, 365))
    res.sort()
    return res

def final():
    count = 0
    i = 0
    random_birthdays()
    for day in res:
        if res[i] == res[i + 1]:
            count = count + 1
            i = i + 1
        else:
            i = i + 1
    return count

def percentage():
    happens = 0
    for i in range(100):
        final()
        happens = happens + count
    percentage = happens / 100

percentage()
但是我得到了这个错误:
对于res中的day:res未定义

我认为我理解Python返回部分的方式确实有问题,因为在我看来,它应该返回res,并在下一行中作为变量使用。

欢迎光临

return需要将返回的值存储在object中

def final():
       count = 0
       i = 0
       res = random_birthdays()
       for day in res:
            if res[i] == res [i+1]:
                 count = count +1
                 i = i+1
            else:
                i = i+1
        return count
欢迎光临

return需要将返回的值存储在object中

def final():
       count = 0
       i = 0
       res = random_birthdays()
       for day in res:
            if res[i] == res [i+1]:
                 count = count +1
                 i = i+1
            else:
                i = i+1
        return count
有点重写:

from collections import Counter
from math import factorial
from random import randrange

def duplicate_birthday(n):
    """
    Given n people with random birthdays,
      do any of them share a birthday?
    """
    c = Counter(randrange(365) for _ in range(n))
    return max(c.values()) > 1

def monte_carlo(n, tries = 1000):
    return sum(1 for _ in range(tries) if duplicate_birthday(n)) / tries

def calculated_odds(n):
    """
    Given n people with random birthdays,
    calculate the odds that at least two share a birthday
    """
    all_combos = 365 ** n
    unique_combos = factorial(365) / factorial(365 - n)
    return 1. - unique_combos / all_combos

def main():
    print("Odds of N people sharing a birthday:")
    for n in range(20, 26):
        sim_pct = 100. * monte_carlo(n)
        calc_pct = 100. * calculated_odds(n)
        print("{:>2d}: {:>4.1f}% ({:>4.1f}%)".format(n, sim_pct, calc_pct))

if __name__ == "__main__":
    main()

Odds of N people sharing a birthday:
20: 42.5% (41.1%)
21: 42.0% (44.4%)
22: 48.8% (47.6%)
23: 50.7% (50.7%)
24: 52.2% (53.8%)
25: 55.9% (56.9%)        
有点重写:

from collections import Counter
from math import factorial
from random import randrange

def duplicate_birthday(n):
    """
    Given n people with random birthdays,
      do any of them share a birthday?
    """
    c = Counter(randrange(365) for _ in range(n))
    return max(c.values()) > 1

def monte_carlo(n, tries = 1000):
    return sum(1 for _ in range(tries) if duplicate_birthday(n)) / tries

def calculated_odds(n):
    """
    Given n people with random birthdays,
    calculate the odds that at least two share a birthday
    """
    all_combos = 365 ** n
    unique_combos = factorial(365) / factorial(365 - n)
    return 1. - unique_combos / all_combos

def main():
    print("Odds of N people sharing a birthday:")
    for n in range(20, 26):
        sim_pct = 100. * monte_carlo(n)
        calc_pct = 100. * calculated_odds(n)
        print("{:>2d}: {:>4.1f}% ({:>4.1f}%)".format(n, sim_pct, calc_pct))

if __name__ == "__main__":
    main()

Odds of N people sharing a birthday:
20: 42.5% (41.1%)
21: 42.0% (44.4%)
22: 48.8% (47.6%)
23: 50.7% (50.7%)
24: 52.2% (53.8%)
25: 55.9% (56.9%)        

返回值,而不是变量
random_birthdies()
是一个计算结果为列表的表达式,就像
2+2
是一个计算结果为整数的表达式一样;您需要对结果执行一些操作(例如将其存储在变量中)。
return res
并不意味着调用者现在有了可以使用的
res
变量<代码>返回返回对象,而不是变量。如果调用者希望将返回的对象保存到变量中,他们必须自己保存。您从未分配变量res。函数返回
res
,但您不维护符号。离开
random_birthdays
功能后,符号不再有意义。您需要将返回值分配给新符号(您可以重用
res
,因为它现在未使用)。将循环前面的行更改为
res=random_birthdays()
res超出范围,您将返回值,而不是变量
random_birthdies()
是一个计算结果为列表的表达式,就像
2+2
是一个计算结果为整数的表达式一样;您需要对结果执行一些操作(例如将其存储在变量中)。
return res
并不意味着调用者现在有了可以使用的
res
变量<代码>返回返回对象,而不是变量。如果调用者希望将返回的对象保存到变量中,他们必须自己保存。您从未分配变量res。函数返回
res
,但您不维护符号。离开
random_birthdays
功能后,符号不再有意义。您需要将返回值分配给新符号(您可以重用
res
,因为它现在未使用)。将循环前的行更改为
res=random_birthdays()
res超出范围有剩余调用。有剩余调用。