在单独文档中的函数循环时,用于操纵布尔值

发布于 2025-01-23 05:59:00 字数 449 浏览 3 评论 0原文

我只是构建一个简单的程序,并且遇到一个没有根据Pylance“访问”的变量。

我知道这通常是为dead Code发生的。

这是我拥有的:

主程序文件。

player_turn = True

while true:
    #some code

我在一个单独的文件中仅用于逻辑函数,我有类似的东西。

def some_func():
    #if some condition
        player_turn = False

但是,在我的功能文件中,Pylance告诉我“未访问player_turn”。

我将变量从主程序导入到我的功能文件中。

而且我从上述文件中的功能正常导入到我的主程序文件中。

有什么建议吗?准备在这里学习新的东西!

提前致谢

I am just building a simple program and I'm encountering a variable not being "accessed" according to Pylance.

I understand that this usually happens for deadcode.

Here is what I have:

Main program file.

player_turn = True

while true:
    #some code

I in a separate file for just logic functions I have something like this.

def some_func():
    #if some condition
        player_turn = False

However, in my function file, pylance is telling me that 'player_turn is not accessed.'

I've imported the variable from my main program into my function file.

And my functions from said file are properly being imported into my main program file.

Any suggestions? Ready to learn something new here!

Thanks in advance

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

谁人与我共长歌 2025-01-30 05:59:00

在此代码中

while true:

不正确
正确的代码是

while player_turn

谢谢

In this code

while true:

Is not correct
Correct code is

while player_turn

Thanks

我的鱼塘能养鲲 2025-01-30 05:59:00

应该提到的第一件事是虽然true循环在没有break语句的情况下永远不会终止。

while True:
    if some_condition is meet: <- conditional statement
        break # <- without this any following_code will not be accessed.

... following_code

另一个问题是,在函数中无法编辑全局范围中定义的不变数据。例如:

a = 5 

def change_a():
    a = 10 # <- this doesn't change a outside of the function. All this
           # does is define a new variable `a` within the function
           # scope which when the function exits will be destroyed
           # not effecting any variables of the same name outside
           # of the function.
 
print(a) # outputs "5"

如果我要删除打印语句,则塔斯语会说a永远不会访问。

The first thing that should be mentioned is that a while True loop never terminates without a break statement.

while True:
    if some_condition is meet: <- conditional statement
        break # <- without this any following_code will not be accessed.

... following_code

Another problem is that immutable data defined in the global scope cannot be edited inside a function. For example:

a = 5 

def change_a():
    a = 10 # <- this doesn't change a outside of the function. All this
           # does is define a new variable `a` within the function
           # scope which when the function exits will be destroyed
           # not effecting any variables of the same name outside
           # of the function.
 
print(a) # outputs "5"

If I were to remove the print statement, pylance would say a was never accessed.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文