尝试在一个”if… else…”语句中调用一个函数,但它说下面的函数没有定义。

huangapple go评论61阅读模式
英文:

Trying to call a function in an "if... else..." statement, and its saying that the function below isn't defined

问题

y = 0
x = 10

print("TRUE" if y > x else theloop())

def theloop():
    while y < 11:
        print(f"Number is {y}!")
        y = y + 1
y = 0
x = 10

def theloop():
    while y < x:
        print(y + 1)

print("true" if y > x else theloop())
英文:
y = 0
x = 10

print(&quot;TRUE&quot; if y&gt;x else theloop())

def theloop():
 while y &lt; 11:
    print(f&quot;Number is {y}!&quot;)
    y = y + 1

I want y to keep on getting bigger by 1 until it becomes bigger than x and print "TRUE", but each time it keeps saying the function isn't defined.

I tried this right here and it seems to work for some reason. But whenever I want y to increase by 1, it completely stops working.

y=0
x=10

def theloop():
    while y &lt; x:
       print(y+1)

print(&quot;true&quot; if y&gt;x else theloop())

答案1

得分: 1

第一件事:

在全局范围内,你需要在调用函数之前定义它。
这解释了为什么你的第一个代码不起作用。Python 还没有定义函数,而你在它有机会访问之前就调用了它。

print("TRUE" if y > x else theloop())

# Python 在你调用函数时还没有看到这个函数
def theloop():
    while y < x:
       print(y+1)

第二件事:

你写的代码只是打印(y+1)...它实际上并没有增加 y 的值。

而且,在函数内部无法访问 y,所以:

在 while 循环之前本地定义它,像这样:

x = 10

def the_loop():
    # 在本地定义 y
    y = 0
    while y < x:
        # 增加 y 的值 1
        y += 1
        print(y)

print("true" if y > x else the_loop())

如果 y 可能会改变,将其作为参数传入:

x = 10
y = 0

# 将 y 作为参数传入
def the_loop(y):
    while y < x:
        # 增加 y 的值 1
        y += 1
        print(y)

print("true" if y > x else the_loop(y))

使用 global 关键字(参见此帖子

y = 0
x = 10

def theloop():
    global y
    while y < x:
       y += 1
       print(y)

print("true" if y > x else the_loop())

使用 global 关键字实际上改变了原始变量,现在 y = 10 可以在函数外部访问。
更多关于全局变量的信息在这里

英文:

First thing:

In the global scope, you want to define a function before you call it.
This explains why your first code doesn't work. Python hasn't defined the function yet and you're calling it before it has a chance to access it.

print(&quot;TRUE&quot; if y&gt;x else theloop())

# Python hasn&#39;t seen this when you call the function
def theloop():
    while y &lt; x:
       print(y+1)

Second thing:

The code you wrote that runs only prints (y+1)... it does not actually increase y by 1.

Also, y is not accessible within the function so:

Define it locally before the while loop like this:

x = 10

def the_loop():
    # define y locally
    y = 0
    while y &lt; x:
        # increases y by 1
        y += 1
        print(y)

print(&quot;true&quot; if y &gt; x else the_loop())

Pass it in as an argument if y might change:

x = 10
y = 0

# pass y in as an argument 
def the_loop(y):
    while y &lt; x:
        # increases y by 1
        y += 1
        print(y)

print(&quot;true&quot; if y &gt; x else the_loop(y))

Use the global keyword (see this post)

y = 0
x = 10

def theloop():
    global y
    while y &lt; x:
       y += 1
       print(y)

print(&quot;true&quot; if y &gt; x else the_loop())

Using global keyword actually changes the original variable and now y = 10 is accessible outside of the function.
More on globals here

huangapple
  • 本文由 发表于 2023年5月13日 10:00:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/76240785.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定