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

问题描述 投票:0回答:1

我正在尝试创建一个使用用户输入的autotyper。对于第一行,autotyper按预期工作,但在第二行,则存在错误。我想知道是不是因为我做的例外?

这是我的完整代码:

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: [email protected]======')
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

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

python python-3.5
1个回答
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,它始终是它开始的时间。

© www.soinside.com 2019 - 2024. All rights reserved.