将多个变量声明为相同的值

时间:2013-12-12 14:09:02

标签: python

使用Python 3.3.3和pywin32 api。

我是python的新手并试图简化我的脚本。我正在尝试为多个变量分配单个值,但它说

  

增强作业的非法表达

import win32api
import win32con
import time

#makes sure your computer doesnt lock when idle for too long...
def move(x, y):
    for i in range(10):
        win32api.SetCursorPos((x,y))

        if(i % 1 == 0): x, y += 10 #this is where it crashes
        if(i % 2 == 0): x, y -= 10

        time.sleep(5)

move(500, 500)

3 个答案:

答案 0 :(得分:2)

if(i % 1 == 0): x, y += 10 #this is where it crashes
if(i % 2 == 0): x, y -= 10

这是不可能的。当您在Python中解包元组时,左侧的变量数必须等于否。在右侧的元组中的项目。

答案 1 :(得分:2)

你可以这样做:

if(i % 1 == 0): x+=10; y+=10
if(i % 2 == 0): x-=10; y-=10

无论如何要小心i%1,它总是会评估为0,因此你的第一个if将始终执行。也许你想这样写它来交替光标移动的方向:

if(i % 2 == 1): x+=10; y+=10
if(i % 2 == 0): x-=10; y-=10

更多阅读:How to put multiple statements in one line?

答案 2 :(得分:1)

您可以将多个简单分配链接在一起,例如:

a = b = 5

但这对于+=这样的扩充作业不起作用。你必须单独写这些。

if i % 1 == 0:
    x += 10
    y += 10

(另外,if不需要括号。)

相关问题