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

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

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

问题

  1. y = 0
  2. x = 10
  3. print("TRUE" if y > x else theloop())
  4. def theloop():
  5. while y < 11:
  6. print(f"Number is {y}!")
  7. y = y + 1
  1. y = 0
  2. x = 10
  3. def theloop():
  4. while y < x:
  5. print(y + 1)
  6. print("true" if y > x else theloop())
英文:
  1. y = 0
  2. x = 10
  3. print(&quot;TRUE&quot; if y&gt;x else theloop())
  4. def theloop():
  5. while y &lt; 11:
  6. print(f&quot;Number is {y}!&quot;)
  7. 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.

  1. y=0
  2. x=10
  3. def theloop():
  4. while y &lt; x:
  5. print(y+1)
  6. print(&quot;true&quot; if y&gt;x else theloop())

答案1

得分: 1

第一件事:

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

  1. print("TRUE" if y > x else theloop())
  2. # Python 在你调用函数时还没有看到这个函数
  3. def theloop():
  4. while y < x:
  5. print(y+1)

第二件事:

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

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

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

  1. x = 10
  2. def the_loop():
  3. # 在本地定义 y
  4. y = 0
  5. while y < x:
  6. # 增加 y 的值 1
  7. y += 1
  8. print(y)
  9. print("true" if y > x else the_loop())

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

  1. x = 10
  2. y = 0
  3. # 将 y 作为参数传入
  4. def the_loop(y):
  5. while y < x:
  6. # 增加 y 的值 1
  7. y += 1
  8. print(y)
  9. print("true" if y > x else the_loop(y))

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

  1. y = 0
  2. x = 10
  3. def theloop():
  4. global y
  5. while y < x:
  6. y += 1
  7. print(y)
  8. 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.

  1. print(&quot;TRUE&quot; if y&gt;x else theloop())
  2. # Python hasn&#39;t seen this when you call the function
  3. def theloop():
  4. while y &lt; x:
  5. 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:

  1. x = 10
  2. def the_loop():
  3. # define y locally
  4. y = 0
  5. while y &lt; x:
  6. # increases y by 1
  7. y += 1
  8. print(y)
  9. print(&quot;true&quot; if y &gt; x else the_loop())

Pass it in as an argument if y might change:

  1. x = 10
  2. y = 0
  3. # pass y in as an argument
  4. def the_loop(y):
  5. while y &lt; x:
  6. # increases y by 1
  7. y += 1
  8. print(y)
  9. print(&quot;true&quot; if y &gt; x else the_loop(y))

Use the global keyword (see this post)

  1. y = 0
  2. x = 10
  3. def theloop():
  4. global y
  5. while y &lt; x:
  6. y += 1
  7. print(y)
  8. 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:

确定