python - 在不让我输入的情况下跳出循环
问题描述
所以我做了一个非常简单的自动打字机,希望能够再次运行或退出它。一切都很好,但最后“结束=输入()”不允许我输入它只是退出程序。有什么理由吗?
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
解决方案
这是一个相当有趣的小问题。正如@Barmar 所指出的,它会发生,因为pyautogui.typewrite()
它正在为您写入控制台。我错误地认为当动作之间没有延迟时它不会发生,这是一个更加令人费解的小问题。
在这种情况下,解决方案很简单:在你之后添加typewrite()
:
if lines:
input()
吸收刚刚为您输入的内容。
推荐阅读
- facebook-messenger - 以编程方式创建 Facebook Messenger 机器人的多个实例?
- jspdf - Jspdf向右浮动项目
- sql-server - 如何在 Visual Studio 2017“查看数据”网格中显示完整的日期时间值?
- arrays - 如何使用 react-native-svg 正确处理折线图上的负值
- ms-access - 使用嵌套查询打开 Recordset
- java - 如何使用 Jackson 将 JSON 数组中的嵌套值解析为列表
- telegram-bot - Telegram-BOT 用户 ID 只是一个私人聊天 ID?
- python - 如何在 python 中修复 datetime TypeError
- android - Android Room:样板代码
- python - Postgresql:使用时间条件插入新记录时更新旧记录