简体   繁体   English

为什么不编程打印值?

[英]Why doesn't program print value?

I am wondering why the following program does not print 'b'. 我想知道为什么以下程序不打印“ b”。 It is is very simple code; 这是非常简单的代码; I think it must work; 我认为它必须起作用; and do not know the reason why it doesn't. 而且不知道为什么不这样做的原因。

def a():
    if b > 10:
        print 'b'
        sys.exit(1)

# main
while 1:
    a()
    b += 1

b is global variable. b是全局变量。 Actual code is more complicated but the structure is the same as mine. 实际代码更复杂,但结构与我的相同。 I guess when I call a() function and if b is greater than 10, it shows 'b'. 我猜想当我调用a()函数时,如果b大于10,它会显示'b'。 However, it does not go inside if-statement. 但是,它不会进入if语句中。

Would you help me out how to solve? 您能帮我解决吗?

Thanks. 谢谢。

Globals are horrid learn not to use them, try something like this 全球人恐惧地学会不使用它们,尝试这样的事情

import sys
def a(value):
    if value > 10:
        print value
        print "Greater than 10!"
        sys.exit(0)
b = 0
while True:
    a(b)
    b += 1

Another answers suggests not using globals, and I agree. 另一个答案表明不使用全局变量,我同意。 If you still want to use globals, you should define b outside of the loop first.(if you do, then please post the complete code, because apart from that, it should work (and it does)). 如果仍要使用全局变量,则应首先在循环外定义b (如果这样做,则请发布完整的代码,因为除此之外,它应该可以工作(并且确实可以))。

Now, global b in the function definition is not necessary, because python guesses it is a global variable when you try to access it before assigning. 现在,函数定义中的global b不再是必需的,因为当您尝试在分配之前尝试访问它时,python会猜测它是一个全局变量。 But since it is not defined it raises an NameError : 但是由于未定义,因此引发了NameError

NameError: global name 'b' is not defined

If you don't see that, so there's something else, you're not showing the actual code that has a problem. 如果没有看到,则说明还有其他问题,您未在显示有问题的实际代码。

This gives you in the end, something similar: 最终,您会得到类似的结果:

import sys
def a():
    global b
    if b > 10:
        print 'b'
        sys.exit(1)

b = 0
# main
while 1:
    a()
    b += 1

just my 2 cents: forget global variables. 只是我的2美分:忘记全局变量。

anyway, this should work 无论如何,这应该工作

def a():
    global b
    if b > 10:
        print 'b'
        sys.exit(1)

EDIT FUUUUUUUUUUUUUUU 编辑 FUUUUUUUUUUUUUUUU

Although I fully agree with Jackob on how you should use functions arguments and avoid globals, just for the record, here's the solution with global: 尽管我完全同意Jackob关于如何使用函数参数并避免使用全局变量的观点,但是为了记录起见,这是使用global的解决方案:

def a():
    global b
    if b > 10:
        print 'b'
        sys.exit(1)

# main
b = 0
while 1:
    a()
    b += 1

You need to define b before "+="ing it. 您需要在“ + =”之前定义b。

def a():
    if b > 10:
        print 'b'
        sys.exit(1)

# main
b = 0 # HERE
while 1:
    a()
    b += 1

By the way, as many had told you: avoid globals. 顺便说一句,正如许多人告诉你的:避免使用全局变量。

Global vars are not the usual way to go. 全局变量不是通常的方法。 You may prefer the usage of nested functions instead: 您可能更喜欢使用嵌套函数

import sys

def outer(value=0):
    count = [value]
    print "started at:", count[0]
    def inner(x=1):
        print "current val:", count[0]
        count[0] += x
        if count[0] > 10:
            print "stopped at:", count[0]
            sys.exit(0)
    return inner

f = outer(5)
while True:
    f(1)

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

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