Python 2.7.10:在函数之间向全局变量添加1

时间:2017-09-10 19:32:50

标签: python

基本上我的代码是这样的:

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

当我运行它时,我没有收到任何错误消息,而是得到了这个:

0

0

[Finished in 0.0s]

我希望得到的是:

1

2

我尝试过添加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

和这个

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

但是我收到了无效的语法错误消息。我很确定唯一可能出错的是全局变量,但我不会因为这就是我认为全局变量的工作方式。

2 个答案:

答案 0 :(得分:3)

  1. 摆脱所有Shader "HoloToolkit/Vertex Lit Configurable" { Properties { [Header(Main Color)] [Toggle] _UseColor("Enabled?", Float) = 0 _Color("Main Color", Color) = (1,1,1,1) [Space(20)] [Header(Base(RGB))] [Toggle] _UseMainTex("Enabled?", Float) = 1 _MainTex("Base (RGB)", 2D) = "white" {} [Space(20)] // Uses UV scale, etc from main texture [Header(Emission(RGB))] [Toggle] _UseEmissionTex("Enabled?", Float) = 0 [NoScaleOffset] _EmissionTex("Emission (RGB)", 2D) = "white" {} [Space(20)] [Header(Blend State)] [Enum(UnityEngine.Rendering.BlendMode)] _SrcBlend("SrcBlend", Float) = 1 //"One" [Enum(UnityEngine.Rendering.BlendMode)] _DstBlend("DestBlend", Float) = 0 //"Zero" [Space(20)] [Header(Other)] [Enum(UnityEngine.Rendering.CullMode)] _Cull("Cull", Float) = 2 //"Back" [Enum(UnityEngine.Rendering.CompareFunction)] _ZTest("ZTest", Float) = 4 //"LessEqual" [Enum(Off,0,On,1)] _ZWrite("ZWrite", Float) = 1.0 //"On" [Enum(UnityEngine.Rendering.ColorWriteMask)] _ColorWriteMask("ColorWriteMask", Float) = 15 //"All" } SubShader { Tags { "RenderType" = "Opaque" "PerformanceChecks" = "False" } LOD 100 Blend[_SrcBlend][_DstBlend] ZTest[_ZTest] ZWrite[_ZWrite] Cull[_Cull] ColorMask[_ColorWriteMask] Pass { Name "FORWARD" Tags { "LightMode" = "ForwardBase" } CGPROGRAM #pragma vertex vert #pragma fragment frag #pragma multi_compile_fwdbase #pragma multi_compile_fog // We only target the HoloLens (and the Unity editor), so take advantage of shader model 5. #pragma target 5.0 #pragma only_renderers d3d11 #pragma shader_feature _USECOLOR_ON #pragma shader_feature _USEMAINTEX_ON #pragma shader_feature _USEEMISSIONTEX_ON #pragma multi_compile __ _NEAR_PLANE_FADE_ON #include "HoloToolkitCommon.cginc" #include "VertexLitConfigurable.cginc" ENDCG } } } 件事,你使用不当。
  2. 编辑global添加到所有相关功能。
  3. global I_like_dem_apples,I_dont_like_dem_apples和所有类似语句替换为return I_like_dem_apples == I_like_dem_apples + 1
  4. 修正您的变量名称,I_like_dem_apples += 1是一个可怕的名字。
  5. 请勿返回I_like_dem_apples"false",请返回"true"False

答案 1 :(得分:0)

通过“return(I_like_dem_apples + 1)”和“return(I_dont_like_dem_apples + 1)”替换前两个函数中的返回值。将“true”和“false”替换为“True”和“False”。你不需要全局变量。