值不相加

2024-06-02 08:07:26 发布

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

好的,这是我的代码:

tick = 1
week = 1
month = 1
year = 1

def new_week(week, tick):
    week = week + 1
    tick = tick + 1
    if tick == 4:
        new_month()
        tick = 1

new_week(week, tick)
print week, tick
new_week(week, tick)
print week, tick

不管以后我告诉它多少次运行new\u week()函数并打印'week'和'tick'的值,它总是为这两个值打印1。。。为什么忽略“week=week+1”行和“tick”行??你知道吗

我正在运行Python2.7 btw


Tags: 函数代码newifdefyearprintweek
3条回答

全局名称weektick被函数参数的名称所掩盖。你知道吗

您需要删除这些参数(因为它们是不必要的),并将weektick声明为函数内的全局参数(这将允许您修改它们的值):

tick = 1
week = 1
month = 1
year = 1

def new_week():
    global week, tick  # Declare that week and tick are global
    week = week + 1    # Modify the value of the global name week
    tick = tick + 1    # Modify the value of the global name tick
    if tick == 4:
        new_month()
        tick = 1

new_week()
print week, tick
new_week()
print week, tick

输出:

2 2
3 3

但是请注意,许多Python程序员认为使用全局声明的函数非常不雅观。一个更具python风格的方法是使用一个值字典,并使用一个函数来递增它们。你知道吗

下面是一个非常简单的例子,让您开始:

dct = {
    'tick': 1,
    'week': 1,
    'month': 1,
    'year': 1
}

def increment(key):
    dct[key] += 1

increment('week')
print dct['week']
increment('tick')
print dct['tick']

输出:

2
2

首先,week = week + 1只是将(本地)名称week重新绑定到一个新值;它不影响调用范围中的变量。但是,由于int是不可变的,所以即使week+=1也不会影响调用范围;它仍然只会更新函数local week变量。你知道吗

一种选择是使用dict而不是单个变量。由于dict是一个可变对象,因此可以将对dict的引用作为参数传递,对该对象的更改将在调用范围中可见。你知道吗

times = { 'tick': 1, 'week': 1, 'month': 1, 'year': 1}

def new_week(t):
   t['week'] += 1
   t['tick'] += 1
   if t['tick'] == 4:
       new_month(t)
       t['tick'] = 1

new_week(times)

这是关于范围的。你知道吗

# here you are setting your variables, there scope is essentially global
tick = 1
week = 1
month = 1
year = 1

# we enter the function definition
def new_week(week, tick):
# you passed in your variables, and increment them
# however, they are only changed within the local scope of this function
# and since you didn't RETURN them as shown below, you are leaving the
# global variables from above, unchanged
week = week + 1
tick = tick + 1
if tick == 4:
    new_month()
    tick = 1

# when you print them out below, you are printing the unchanged globally scoped variables
new_week(week, tick)
print week, tick 
new_week(week, tick)
print week, tick

为了获得所需的结果,您需要:

tick = 1
week = 1
month = 1
year = 1

def new_week(week, tick):
    week += 1
    tick += 1
    if tick == 4:
        new_month()
        tick = 1
    return (week, tick)  #### return the variables

>>> week, tick = new_week(week, tick) ### capture the returned variables
>>> print week, tick
(2, 2)

tickweek在每次调用new_week时都将继续递增。显然,tick一旦达到4,就会重置。如果要将变量存储在listdict中,则可以执行以下操作:

使用dict

vars = {'tick': 1, 'week': 1, 'month': 1, 'year': 1}

这将改变函数处理数据的方式:

    def new_week(lst=None):
        if lst:
            for key in lst:
                vars[key] += 1
            if vars['tick'] == 4:
                new_month()
                vars['tick'] = 1

>>> new_week(['week', 'tick'])
>>> print vars['week'], vars['tick']
2 2

请在此处阅读有关范围的内容:

https://www.inkling.com/read/learning-python-mark-lutz-4th/chapter-17/python-scope-basics

http://www.python-course.eu/python3_global_vs_local_variables.php

相关问题 更多 >