【问题标题】:Function can't recognize global variable函数无法识别全局变量
【发布时间】:2017-05-13 21:07:41
【问题描述】:

我编写了一个 Python 脚本,它根据弹丸的速度和角度显示其覆盖的距离。

但是,它不会读取另一个函数 (ask_values()) 从用户返回的这两个变量。

是什么阻止了covered_distance() 函数读取用户在ask_values() 中输入的2 个变量(theta 和velocity)?

这是程序输出:

Projectile angle:   10
Velocity:       10
Angle: 10.0 Speed: 10.0
Angle: 0.0 Speed: 0.0 

Distance covered: 0.0

这是程序本身:

# IMPORT MODULES
from math import *          # Import all methods/attributes from math module
import sys                  # Import sys.exit()

# DECLARE VARIABLES
theta = 0.0                 # theta = angle at which the projectile is launched
velocity = 0.0              # velocity = speed of the projectile
percent_distance_var = 0.0  # percent_distance_var = percentage of the covered distance
max_distance_var = 0.0      # max_distance_var = maximum distance
covered_distance_var = 0.0  # covered_distance_var = covered distance

# Covered distance
def covered_distance(theta_, velocity_, covered_distance_var_):   # Arguments: (theta, speed, covered_distance_var)
    covered_distance_var_ = 0.2041*((velocity_)**2)*sin(theta_)*cos(theta_) # Calculate 0.2041*((velocity)**2)*sin(theta)*cos(theta)
    data = dict(angle=theta_, speed=velocity_, distance=covered_distance_var_)
    print("Angle: {angle} Speed: {speed} \n \nDistance covered_: {distance}".format(**data)) # Doesn't print out the correct variables
    return covered_distance_var_         # Return covered_distance_var

# Ask user for values
def ask_values(theta, velocity):
    theta = float(input("Projectile angle: \t"))
    velocity = float(input("Velocity: \t \t"))
    print("Angle: {} Speed: {}".format(theta, velocity)) # Prints out the correct variables
    return(theta, velocity)


def main():# Main method
    ask_values(theta, velocity) # Ask for values
    covered_distance(theta, velocity, covered_distance_var)

# EXECUTE CODE
if __name__ == "__main__":   # If "__main__" statement
    main()  # Main() method

【问题讨论】:

  • 函数中可以使用global some_global_variabe访问全局变量
  • @NipunGarg 我强烈建议不要在您的所有功能中使用global。上面的函数基本上都可以使用,只需将输出分配给正确的名称即可。
  • @juanpa.arrivillaga 在这种情况下,使用全局变量在这里毫无用处。
  • @NipunGarg 我同意。

标签: python python-3.x global-variables physics local-variables


【解决方案1】:

您必须在main 中捕获函数返回的值,否则它们将被丢弃并且永远不会进入命名空间。当您访问该名称时,它不会在本地找到任何内容,而是查看参数的全局值。所以不要这样:

def main():# Main method
    ask_values(theta, velocity) # Ask for values
    covered_distance(theta, velocity, covered_distance_var)

做:

def main():# Main method
    theta, velocity = ask_values(theta, velocity) # Ask for values
    covered_distance(theta, velocity, covered_distance_var)

否则thetavelocity 值将对应于您在此处定义的值:

# DECLARE VARIABLES
theta = 0.0                 # theta = angle at which the projectile is launched
velocity = 0.0              # velocity = speed of the projectile

在全局命名空间中。如果您无论如何都想接受用户输入,那么在这里为这些变量赋值是毫无用处的。 Python 不是具有变量声明概念的静态类型语言。变量在分配时“立即存在”。

另外,你可能想要print你的最终结果:

print(covered_distance(theta, velocity, covered_distance_var))

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-03-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-23
    • 1970-01-01
    • 2017-03-27
    相关资源
    最近更新 更多