首页 > 解决方案 > 引用变量时出现问题 - 可能是全局状态问题

问题描述

以下代码位于循环多次(由 t 定义)的函数定义中。在任何后续时间 t > 1,我想查看当前价格是否比起始价格低 20% 或更多。但是,我收到“分配前引用的局部变量”错误,因为 if 语句中的 start_price 有问题。我也尝试在多个地方使用“global start_price”。任何想法或想法都会有所帮助。

if t = 1:
    start_price = a / b

current_price = a / b

if current_price / start_price <= 0.80:
    print("Price has dropped 20% or more")

标签: global

解决方案


推荐阅读