猿问

TypeError: 'int' 对象在 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: 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)

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


ITMISS
浏览 228回答 1
1回答

森栏

错误是因为这段代码:try:&nbsp; &nbsp; line2 = 1except NameError:&nbsp; &nbsp; breakelse:&nbsp; &nbsp; for char in line2:&nbsp; &nbsp; &nbsp; &nbsp; keyboard.press(char)&nbsp; &nbsp; &nbsp; &nbsp; keyboard.release(char)&nbsp; &nbsp; &nbsp; &nbsp; time.sleep(0.1)之后line2 = 1,line2不再包含用户输入的行,而是包含整数1。for char in line2将不再有效,因为您无法遍历整数。我认为您正在尝试使用该try/except块来判断用户是否为line2. 但是赋值永远不会报告 a NameError,只有在尝试读取尚未赋值的变量时才会出现该错误。此外,读取输入的代码始终分配给line2,因此您不会收到错误消息。如果您为前几行输入,则可能未设置的变量是line3and 。line4none每当您发现自己创建的变量具有这样的顺序名称时,您可能应该使用列表而不是单独的变量。以下是读取所有输入行的方法:lines = []while True:&nbsp; &nbsp; line = input("Please enter your next line, type 'none' if you don't have one")&nbsp; &nbsp; if line == "none":&nbsp; &nbsp; &nbsp; &nbsp; break&nbsp; &nbsp; lines.append(line)当你想输入它们时:random.seed()while time.time() < future and runs < hours*60:&nbsp; &nbsp; for line in lines:&nbsp; &nbsp; &nbsp; &nbsp; for char in line:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; keyboard.press(char)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; keyboard.release(char)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; time.sleep(0.1)&nbsp; &nbsp; &nbsp; &nbsp; keyboard.press(Key.shift)&nbsp; &nbsp; &nbsp; &nbsp; keyboard.press(Key.enter)&nbsp; &nbsp; &nbsp; &nbsp; keyboard.release(Key.shift)&nbsp; &nbsp; &nbsp; &nbsp; keyboard.release(Key.enter)&nbsp; &nbsp; runs += 1&nbsp; &nbsp; print("Random Seconds Added:")&nbsp; &nbsp; print(int(random1))&nbsp; &nbsp; print("Time to next entry:")&nbsp; &nbsp; for y in range(int(retry) + int(random1)):&nbsp; &nbsp; &nbsp; &nbsp; print(int(retry) + int(random1 - y))&nbsp; &nbsp; &nbsp; &nbsp; time.sleep(1)另一个问题是您timeout在循环期间从未更新,它始终是它开始的时间。
随时随地看视频慕课网APP

相关分类

Python
我要回答