f.write 只写不变的变量?

f.write only writes unchanged variables?

这是我总结的代码:

Var1 = 10
Var2 = 100
Var3 = 300
Var4 = 0

Amount = input("Blahblahblah amount for Var 4: ")
Amount2 = input("Blahblahblah amount for Var 1: ")

Amount2 + Amount * Var3 == Var4
Var4 - Var2 == Amount2
 if Amount2 == 20:
     Amount - Var4
 else:
     Amount + Var4 == Var 2

Y = input("Type 'Y' to continue:")
if (Y == "Y"):
    try:
       with open("Test.txt", "a") as f:
           f.write(f"Var 2 == {Var2}, Var 4 == {Var4} \n")
        except Exception as e:
            print(e)

问题是在 Test.txt 中它写 Var 2 == 100, Var 4 == 0 代替了真正的答案。 所以无论我在 'Amount' 或 'Amount 2' 中写什么,它总是 = 到 Var 2 == 100, Var 4 == 0。 这是我看不到的愚蠢错误吗?

在这些行中,您没有为变量重新赋值。

Amount2 + Amount * Var3 == Var4
Var4 - Var2 == Amount2
if Amount2 == 20: 
   Amount - Var4
else:
   Amount + Var4 == Var 2

要将值重新分配给变量,请使用:

# definition
a = 1
b = 10
# reassignment
a = a + b
# the new value of a will be 11
# a = 1 + 10

此外,避免用大写字母调用变量(这应该只在定义类时使用)。