TypeError:“ int”对象在Autotyper中不可迭代

时间:2019-03-09 00:06:10

标签: python python-3.5

我正在尝试创建一个使用用户输入的自动打字机。 对于第一行,自动打字机按预期工作,但是在第二行,则出现错误。我想知道是否是因为我的例外?

这是我完整的代码:

import time
import random
from goto import with_goto

keyboard = Controller()
class label(Exception): pass

print('======Welcome to Autotyper v0.5 Currently still in development. We have a maximum of 4 lines, for bugs/suggestions email: alexanderhan00@gmail.com======')
while True:
    line1 = input("Please enter your first line:  ")
    line2 = input("Please enter your second line, type none if you don't have one:  ")
    if line2 == "none":
        break
    line3 = input("Please enter your third line, type none if you don't have one:  ")
    if line3 == "none":
        break
    line4 = input("Please enter your fourth line, This is the last line!!  ")
    break

hours = input("How many hours would you like to run this?     ")
retry = input("How long will it take until new message?(Seconds)     ")
timeout = round(time.time())
future = round(time.time()) + int(60)*int(60)*int(hours)
x = 5
print('Move cursor to target')
while x > 0:
    print(int(x)*'.')
    time.sleep(1)
    x = x-1
runs = 0

while True:
    random.seed()
    random1 = random.randint(0,10)
    for char in line1:
        keyboard.press(char)
        keyboard.release(char)
        time.sleep(0.1)
    keyboard.press(Key.shift)
    keyboard.press(Key.enter)
    keyboard.release(Key.shift)
    keyboard.release(Key.enter)
    try:
        line2 = 1
    except NameError:
        break
    else:
        for char in line2:
            keyboard.press(char)
            keyboard.release(char)
            time.sleep(0.1)
        keyboard.press(Key.shift)
        keyboard.press(Key.enter)
        keyboard.release(Key.shift)
        keyboard.release(Key.enter)
    runs = runs + 1
    if timeout > future or runs == hours*60:
        break
    print("Random Seconds Added:")
    print(int(random1))
    print("Time to next entry:")
    for y in range(int(retry) + int(random1)):
        print(int(retry) + int(random1 - y))
        time.sleep(1)

运行它时,得到以下输出:

Please enter your first line:  hi
Please enter your second line, type none if you don't have one:  none
How many hours would you like to run this?     1
How long will it take until new message?(Seconds)     10
Move cursor to target
.....
....
...
..
.
Traceback (most recent call last):
  File "C:\Users\alexa\Desktop\autotyper-random.py", line 49, in <module>
    for char in line2:
TypeError: 'int' object is not iterable
hi

非常感谢您的帮助,谢谢!我是社区的新手。

1 个答案:

答案 0 :(得分:0)

该错误是由于以下代码引起的:

try:
    line2 = 1
except NameError:
    break
else:
    for char in line2:
        keyboard.press(char)
        keyboard.release(char)
        time.sleep(0.1)

line2 = 1之后,line2不再包含用户输入的行,而是包含整数1for char in line2将不再起作用,因为您无法循环访问整数。

我认为您正在尝试使用try/except块来判断用户是否为line2输入了内容。但是赋值永远不会报告NameError,只有在尝试读取尚未赋值的变量时,您才能得到该错误。另外,读取输入的代码始终分配给line2,因此不会出错。如果您为前几行输入了line3,则可能无法设置的变量为line4none

每当找到具有这样的顺序名称的自创建变量时,就应该使用列表而不是单独的变量。在所有输入行中阅读的方法如下:

lines = []
while True:
    line = input("Please enter your next line, type 'none' if you don't have one")
    if line == "none":
        break
    lines.append(line)

当您要键入它们时:

random.seed()
while time.time() < future and runs < hours*60:
    for line in lines:
        for char in line:
            keyboard.press(char)
            keyboard.release(char)
            time.sleep(0.1)
        keyboard.press(Key.shift)
        keyboard.press(Key.enter)
        keyboard.release(Key.shift)
        keyboard.release(Key.enter)
    runs += 1
    print("Random Seconds Added:")
    print(int(random1))
    print("Time to next entry:")
    for y in range(int(retry) + int(random1)):
        print(int(retry) + int(random1 - y))
        time.sleep(1)

另一个问题是,您永远不会在循环中更新timeout,这总是它开始的时间。