首页 > 解决方案 > 在不让我输入的情况下跳出循环

问题描述

所以我做了一个非常简单的自动打字机,希望能够再次运行或退出它。一切都很好,但最后“结束=输入()”不允许我输入它只是退出程序。有什么理由吗?

import pyautogui
import time
import os


def clearConsole():
    command = 'clear'
    if os.name in ('nt', 'dos'):  # If Machine is running on Windows, use cls
        command = 'cls'
    os.system(command)

break_loop = 1

while break_loop <= 1:
    secs_inbetween_actions = float(input("""
Seconds between actions(should be float but int is also ok): """))
    no_of_lines = int(input("""
How many Lines do you want to write?(Only int): """))
    time_between_action = int(input("""
How long should the time between enter and writing be?: """))
    lines = ""
    print("""
Write your Text under this message (You have """ + str(no_of_lines) + """ line(s) to wite)
""")
    for i in range(no_of_lines):
        lines += input() + "\n"

    print("-------------------------------------")

    while time_between_action > 0:
        time_between_action = time_between_action - 1
        print('Time Left until writing -+==> ' + str(time_between_action))
        time.sleep(1)

    print("-------------------------------------")
    pyautogui.typewrite(lines, interval=secs_inbetween_actions)

    ending = input("If you want to use this aplication once again type 'y' + 'enter key' ,if not press the 'enter key': ")

    if ending == "y":
        break_loop = 1
        clearConsole()
    else:
        break_loop += 1

标签: python

解决方案


这是一个相当有趣的小问题。正如@Barmar 所指出的,它会发生,因为pyautogui.typewrite()它正在为您写入控制台。我错误地认为当动作之间没有延迟时它不会发生,这是一个更加令人费解的小问题。

在这种情况下,解决方案很简单:在你之后添加typewrite()

if lines:
    input()

吸收刚刚为您输入的内容。


推荐阅读