为什么当我增加变量时这给我0,0

时间:2020-10-19 16:37:11

标签: python logic

我试图增加变量a和b,所以当我键入它们并按Enter时, 变量a和b增加一。我的目的是当我完成并想要一个和
打印我会键入除a和b以外的任何东西,并获取变量的值。因此, 我写了以下代码:

def increment():
    a = 0
    b = 0
    c = 0
    d = 0
    e = 0
    f = 0
    g = 0
    h = 0
    i = 0
    j = 0
    k = 0
    l = 0
    m = 0
    n = 0
    o = 0
    p = 0
    q = 0
    r = 0
    s = 0
    t = 0
    u = 0
    v = 0
    w = 0
    x = 0
    y = 0
    z = 0

    statAsk = input("Enter your letter, else type go for overall statistics ")
    if statAsk == "a":
        a = a+1
        increment()
    elif statAsk == "b":
        b = b+1
        increment()
   else:
       print(a,b)

increment()

但是,当我运行它时,a和b根本没有增加,我得到了:

Enter your letter, else type go for overall statistics a
Enter your letter, else type go for overall statistics b
Enter your letter, else type go for overall statistics b
Enter your letter, else type go for overall statistics a
Enter your letter, else type go for overall statistics b
Enter your letter, else type go for overall statistics go
0 0

如何解决此逻辑错误?

5 个答案:

答案 0 :(得分:0)

在该函数内部,仅更新局部变量,不涉及全局变量。

因此,要更改全局变量,请先使用global variable_name,然后再进行如下更改:

if statAsk == "a":
        global a
        a = a+1
        increment()

依此类推...

答案 1 :(得分:0)

使用python全局关键字,每次调用该函数时,您的值都将重置为零。因此,在函数外声明变量,您的 increment()函数应该是公正的

def increment():
   

    statAsk = input("Enter your letter, else type go for overall statistics ")
    if statAsk == "a":
        a = a+1
        increment()
    elif statAsk == "b":
        b = b+1
        increment()
    else:
        print(a,b)

increment()

有关全局关键字的更多信息,可以参考: W3Schools

这只是众多的其中之一

答案 2 :(得分:0)

正如@schwobaseggl所述,要递增的变量a,b,c,...,z在该函数中是局部的。这些变量仅在该函数执行期间存在。当您退出增量功能时,这些变量将被破坏。

但是,当您下次调用增量函数时,您将再次定义局部变量。每次运行此功能时,都将重置值。当您设置a = 0,b = 0等时,可以在增量函数中看到这一点。

您要做的是保留这些值。这可以通过在函数外部定义值来完成,此处定义的任何变量都将具有全局作用域。

a = 0
b = 0
c = 0

#In order to access these variables above from inside a function, we can use the global keyword

def increment():
    ##Now we need to be able to access the global scope from this local scope (that is the scope of the function
    ##This global keyword means that we are accessing the variable a defined int eh global scope
    global a
    ##Run your logic
    if condition is met:
        a = a+1
    

我会避免使用global关键字,而只是通过增量函数将变量作为参数传递

有关python https://www.w3schools.com/PYTHON/python_scope.asp

中的作用域的更多信息

答案 3 :(得分:0)

您使用递归方法。然后,您递归的函数应始终返回值。返回的值应添加到您已有的结果中。

folderOne
|__ fileOne.ts
folderTwo
|__ fileTwo.ts
folderThree
|__ subfolderA
    |__ fileThree.ts

结果

def increment():
    a = 0
    b = 0
    c = 0
    d = 0
    e = 0
    f = 0
    g = 0
    h = 0
    i = 0
    j = 0
    k = 0
    l = 0
    m = 0
    n = 0
    o = 0
    p = 0
    q = 0
    r = 0
    s = 0
    t = 0
    u = 0
    v = 0
    w = 0
    x = 0
    y = 0
    z = 0

    statAsk = input("Enter your letter, else type go for overall statistics ")
    if statAsk == "a":
        a += 1
        a_result, b_result = increment()
        a += a_result
        b += b_result
    elif statAsk == "b":
        b += 1
        a_result, b_result = increment()
        a += a_result
        b += b_result

    return a, b


a_result, b_result = increment()

print(a_result, b_result)

答案 4 :(得分:0)

def increment():
a = 0
b = 0
statAsk=''
while(statAsk !='x'): # Exit the program if you write
    statAsk = input("Enter your letter, else type go for overall ")
    if statAsk == "a":
         a+=1
    elif statAsk == "b":
         b+=1
return a ,b