尝试写入文件时缺少变量

时间:2019-10-17 13:00:39

标签: python python-3.7

有人可以看到我要去哪里了吗?我只是将变量写入文件中以检查一切是否如预期。一切正常,除了“ y”。为什么不将“ y”写入文件?可能很明显,但我看不到错误。在python shell中工作,但不在我的脚本中工作。 RPi3上的Python 3.7。

-7.-70 xVal is -7 yVal is -70 Y is
-7.-70 xVal is -7 yVal is -70 Y is
-7.-70 xVal is -7 yVal is -70 Y is
0.0 xVal is 0 yVal is 0 Y is

输出到debug.txt

post = Post(
    #author = author_id, # Python complains about author's type, which is int and should be User
    author = User[author_id], # This works, but as I understand it actually fetches the user object
    #author = User(id=author_id), # This will try and (fail to) create a new record for the User table when I commit, am I right?
    # ...
)

在我发疯之前,我将不胜感激!

1 个答案:

答案 0 :(得分:4)

值是字符串。您需要先将它们转换为整数,然后再乘以它们:

y = int(yVal)*-1

但是为什么现在是空的?因为字符串* n使该字符串重复n次。负数被认为与0相同,从而使y为空字符串。