尽管我的局部变量在函数中被调用,但为什么我的局部变量的值没有变化?

时间:2019-02-14 04:10:59

标签: python function variables turtle-graphics

在我要创建的程序中,我正在使用Turtle创建一个12小时的数字闹钟,其中每个数字(hh:mm)都会改变,就好像时钟要经过24小时一样。但是,我用来设置分钟数和所在位置的时针的一些变量的值并没有改变,即使我已经设置它们可以在满足某些条件时加起来。

因此,基本上,我已经设置了乌龟画一张桌子,并在那张桌子上摆着钟表的身体和脸。但更重要的是,我制造了6只海龟,一只代表小时数和分钟数之间的点,一只代表AM / PM,另一只代表每个数字。它们都是由我上面描述的每只海龟的单独功能绘制的。我有一个单独的.txt文档,每行5位,对应于每只数字指针需要更改的点。使用AM / PM和十位小时数功能,我建立了一个基本的if和else语句,如果与该特定功能相对应的位为0,则十位将为0,或如果该位为1,则其为1。但是,对于其余的数字,如果该位为0,则相应的函数将重复其所做的最后绘制,并保存在自己的图中功能。如果该位为1,则将其保存在变量中,然后将其与另一个变量一起用于模运算,该变量将产生与下一个循环或旋转数相对应的数字。例如,对于某人的分针,可以出现的数字为0、1、2、3、4、5、6、7、8和9。如果模运算结果为3,则数字将为同样是3。将3保存在变量中,以便当该位显示为0时,该函数可以显示之前的数字。同样,如果模数运算结果为0,则我在模数函数中描述的第二个变量每次都会添加一定的数字。在一个人的分针的情况下,该数字为10。仅此而已。我的代码如下:

#Program Setup
import turtle
import time
num = turtle.Turtle()
num2 = turtle.Turtle()
num3 = turtle.Turtle()
num4 = turtle.Turtle()
text = turtle.Turtle()
binlist = []
com = 12
com1 = 6
com2 = 10
bits = 0
bits1 = 0
bits2 = 0
#Function Setup
def hour1(bit1,g):
    if int(bit1) == 0:
        num.clear()
        g = num.write("0",False,"center",("alarm clock",100,"normal"))
    else:
        num.clear()
        g = num.write("1",False,"center",("alarm clock",100,"normal"))
def hour2(bit2,g1,bits,com):
    if int(bit2) == 0:
        num2.clear()
        g1
    else:
        bits +=1
        return bits
        if int(bits) % int(com) == 1:
            num2.clear()
            g1 = num2.write("1",False,"center",("alarm clock",100,"normal"))
        elif int(bits) % int(com) == 2:
            num2.clear()
            g1 = num2.write("2",False,"center",("alarm clock",100,"normal"))
        elif int(bits) % int(com) == 3:
            num2.clear()
            g1 = num2.write("3",False,"center",("alarm clock",100,"normal"))
        elif int(bits) % int(com) == 4:
            num2.clear()
            g1 = num2.write("4",False,"center",("alarm clock",100,"normal"))
        elif int(bits) % int(com) == 5:
            num2.clear()
            g1 = num2.write("5",False,"center",("alarm clock",100,"normal"))
        elif int(bits) % int(com) == 6:
            num2.clear()
            g1 = num2.write("6",False,"center",("alarm clock",100,"normal"))
        elif int(bits) % int(com) == 7:
            num2.clear()
            g1 = num2.write("7",False,"center",("alarm clock",100,"normal"))
        elif int(bits) % int(com) == 8:
            num2.clear()
            g1 = num2.write("8",False,"center",("alarm clock",100,"normal"))
        elif int(bits) % int(com) == 9:
            num2.clear()
            g1 = num2.write("9",False,"center",("alarm clock",100,"normal"))
        elif int(bits) % int(com) == 10:
            num2.clear()
            g1 = num2.write("0",False,"center",("alarm clock",100,"normal"))
        elif int(bits) % int(com) == 11:
            num2.clear()
            g1 = num2.write("1",False,"center",("alarm clock",100,"normal"))
        else:
            num2.clear()
            g1 = num2.write("2",False,"center",("alarm clock",100,"normal"))
            com += 12
            return com
def min1(bit3,g2,bits1,com1):
    if int(bit3) == 0:
        num3.clear()
        g2
    else:
        bits1 += 1
        return bits1
        if int(bits1) % int(com1) == 1:
            num3.clear()
            g2 = num3.write("1",False,"center",("alarm clock",100,"normal"))
        elif int(bits1) % int(com1) == 2:
            num3.clear()
            g2 = num3.write("2",False,"center",("alarm clock",100,"normal"))
        elif int(bits1) % int(com1) == 3:
            num3.clear()
            g2 = num3.write("3",False,"center",("alarm clock",100,"normal"))
        elif int(bits1) % int(com1) == 4:
            num3.clear()
            g2 = num3.write("4",False,"center",("alarm clock",100,"normal"))
        elif int(bits1) % int(com1) == 5:
            num3.clear()
            g2 = num3.write("5",False,"center",("alarm clock",100,"normal"))
        else:
            num3.clear()
            g2 = num3.write("0",False,"center",("alarm clock",100,"normal"))
            com1 += 6
            return com1
def min2(bit4,g3,bits2,com2):
    if int(bit4) == 0:
        g3
    else:
        bits2 += 1
        return bits2
        if int(bits2) % int(com2) == 1:
            num4.clear()
            g3 = num4.write("1",False,"center",("alarm clock",100,"normal"))
        elif int(bits2) % int(com2) == 2:
            num4.clear()
            g3 = num4.write("2",False,"center",("alarm clock",100,"normal"))
        elif int(bits2) % int(com2) == 3:
            num4.clear()
            g3 = num4.write("3",False,"center",("alarm clock",100,"normal"))
        elif int(bits2) % int(com2) == 4:
            num4.clear()
            g3 = num4.write("4",False,"center",("alarm clock",100,"normal"))
        elif int(bits2) % int(com2) == 5:
            num4.clear()
            g3 = num4.write("5",False,"center",("alarm clock",100,"normal"))
        elif int(bits2) % int(com2) == 6:
            num4.clear()
            g3 = num4.write("6",False,"center",("alarm clock",100,"normal"))
        elif int(bits2) % int(com2) == 7:
            num4.clear()
            g3 = num4.write("7",False,"center",("alarm clock",100,"normal"))
        elif int(bits2) % int(com2) == 8:
            num4.clear()
            g3 = num4.write("8",False,"center",("alarm clock",100,"normal"))
        elif int(bits2) % int(com2) == 9:
            num4.clear()
            g3 = num4.write("9",False,"center",("alarm clock",100,"normal"))
        else:
            num4.clear()
            g3 = num4.write("0",False,"center",("alarm clock",100,"normal"))
            com2 += 10
            return com2
def meridiem(bit5,g4):
    if int(bit5) == 0:
        text.clear()
        g4 = text.write("AM",False,"center",("alarm clock",25,"normal"))
    else:
        text.clear()
        g4 = text.write("AM",False,"center",("alarm clock",25,"normal"))
f = open("ikeokereke.txt","r")
for line in f:
    binlist.append(line.rstrip('\n'))
for e in binlist:
    bit1 = e[0:1]
    bit2 = e[1:2]
    bit3 = e[2:3]
    bit4 = e[3:4]
    bit5 = e[4:5]
    time.sleep(.5)
    hour1(bit1,g)
    hour2(bit2,g1,bits,com)
    min1(bit3,g2,bits1,com1)
    min2(bit4,g3,bits2,com2)
    meridiem(bit5,g4)
    print(bits)
    print(bits1)
    print(bits2)
    print(com)
    print(com1)
    print(com2)

预期结果:正常运行的数字闹钟 实际结果:十个位置的时针显示为0,一个十个位置的分针显示为0,而其他两个数字甚至都没有显示。

ikeokereke.txt

https://docs.google.com/document/d/e/2PACX-1vT6O_NqKrKR0tC8IPvoItwmJL0uCgbUJsBK0xH62IHAfx_nJxD62DuKjc8Ih93YyMKsNGn0EP6Nxu6Q/pub

2 个答案:

答案 0 :(得分:0)

即使这小段代码也有很多问题:

def hour2(bit2,g1,bits,com):
    if int(bit2) == 0:
        num2.clear()
        g1
    else:
        bits +=1
        return bits
        if int(bits) % int(com) == 1:
            ...

特别是:

  • g1从未在任何地方定义,因此呼叫hour2(bit2,g1,bits,com)无法正常工作。

  • g1是否有任何可能的值,可以将g1本身作为代码的一部分放在一行上?

  • 递增bits并返回它是没有意义的,因为没有任何东西看待hour2()调用的返回值,并且如果bits被用作全局变量,则此无法使用,因为该函数中没有global语句。

  • return bits语句之后的三十行代码将永远不会执行,因为您离开了函数。

  • bitscom都已经是整数,因此一遍又一遍地调用int()似乎表明您对自己的设计缺乏了解。

答案 1 :(得分:0)

我能够通过在for循环中而不是函数本身中增加变量来解决此问题。代码中的更改如下:

# filter Admsn_ctr_crs_cd to only show 300XXXX for UG or PG 860XXXX
selector = abc['column_title_A'].str.startswith("300") | abc['column_title_A'].str.startswith("860")

# change value based on another value in another
abc.loc[(abc['column_title_B'] == '29JUL2019') & selector, 'column_title_C'] = '15/02/2019'
abc.loc[(abc['column_title_B'] == '25FEB2019') & selector, 'column_title_C'] = '19/05/2019'

# from here on, how do I unfilter the above to apply another filter below?
selector = abc['column_title_B'].str.startswith("300") | abc['column_title_B'].str.startswith("860")
abc.loc[otherselector & selector, "column_title_D"] = "foo"