简体   繁体   English

Python 2.7.10:在函数之间的全局变量中加1

[英]Python 2.7.10: Adding 1 to global variables between functions

So basically my code is this: 所以基本上我的代码是这样的:

global I_like_dem_apples
I_like_dem_apples = 0
global I_dont_like_dem_apples 
I_dont_like_dem_apples = 0

def how_do_you_like_them_applez(steve_jobs):
    if steve_jobs == "Empire" or steve_jobs == "Gala":
        return I_like_dem_apples == I_like_dem_apples + 1

    if steve_jobs == "Rome" or steve_jobs == "York":
        return I_dont_like_dem_apples == I_dont_like_dem_apples + 1

    else:
        return I_dont_like_dem_apples == I_dont_like_dem_apples + 1

def good_day (the_good_stuff):
    for apple in the_good_stuff:
        how_do_you_like_them_applez(apple)
    if I_dont_like_dem_apples > I_like_dem_apples or I_dont_like_dem_apples == I_like_dem_apples:
        return "false"

    if I_like_dem_apples > I_dont_like_dem_apples:
        return "true"

good_day(["Gala", "York", "Rome"])
print I_like_dem_apples 
print I_dont_like_dem_apples

When I run this, I get no error messages, but instead get this: 当我运行它时,我没有收到任何错误消息,而是得到了:

0

0

[Finished in 0.0s]

What I was hoping to get is: 我希望得到的是:

1

2

I've tried changing the code around adding 1 a bit, 我已经尝试过将代码加1左右来更改代码,

def how_do_you_like_them_applez(steve_jobs):
    if steve_jobs == "Empire" or steve_jobs == "Gala":
        return I_like_dem_apples =+ 1

    if steve_jobs == "Rome" or steve_jobs == "York":
        return I_dont_like_dem_apples =+ 1

    else:
        return I_dont_like_dem_apples =+ 1

and this 和这个

def how_do_you_like_them_applez(steve_jobs):
    if steve_jobs == "Empire" or steve_jobs == "Gala":
        return I_like_dem_apples = I_like_dem_apples + 1

    if steve_jobs == "Rome" or steve_jobs == "York":
        return I_dont_like_dem_apples = I_dont_like_dem_apples + 1

    else:
        return I_dont_like_dem_apples = I_dont_like_dem_apples + 1

but I get the invalid syntax error message. 但我收到无效的语法错误消息。 I'm pretty sure the only things that could be wrong are the global variables, but I wouldn't because that's how I thought global variables worked. 我很确定唯一可能出错的是全局变量,但是我不会,因为那是我认为全局变量起作用的方式。

  1. Get rid of all global things, you use them incorrectly. 摆脱所有global东西,您会错误地使用它们。
  2. EDIT Add global I_like_dem_apples,I_dont_like_dem_apples to all involved functions. 编辑global I_like_dem_apples,I_dont_like_dem_apples添加到所有涉及的函数中。
  3. Replace return I_like_dem_apples == I_like_dem_apples + 1 and all similar statements with I_like_dem_apples += 1 . return I_like_dem_apples == I_like_dem_apples + 1和所有类似的语句替换为I_like_dem_apples += 1
  4. Fix your variable names, I_like_dem_apples is a horrible name. 修正您的变量名, I_like_dem_apples是一个可怕的名字。
  5. Do not return "false" and "true" , return False and True . 不要返回"false""true" ,返回FalseTrue

Replace the returns in the first two functions through "return(I_like_dem_apples + 1)" and "return(I_dont_like_dem_apples + 1)". 通过“ return(I_like_dem_apples + 1)”和“ return(I_dont_like_dem_apples + 1)”替换前两个函数中的return。 Replace "true" and "false" through "True" and "False". 通过“ True”和“ False”替换“ true”和“ false”。 You don't need the globals. 您不需要全局变量。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM