python似乎无缘无故地跳过了一行

python似乎无缘无故地跳过了一行,python,Python,我正在写一些代码,我有一个问题。我编写的函数运行良好,但主循环只能正常工作一次。代码如下: while 1==1: choice=int(raw_input("press 1 for encrypt, 2 for decrypt")) if choice==1: string=raw_input("please enter plaintext here\n") print('cipher-text follows and was copied to

我正在写一些代码,我有一个问题。我编写的函数运行良好,但主循环只能正常工作一次。代码如下:

while 1==1:
    choice=int(raw_input("press 1 for encrypt, 2 for decrypt"))
    if choice==1:
        string=raw_input("please enter plaintext here\n")
        print('cipher-text follows and was copied to clipboard "'+encrypt_string(string))+'"'
    elif choice==2:
        string=raw_input("please enter cipher-text here\n")
        print('plaintext follows : "'+decrypt_string(string))+'"'
    else:
        print("please enter a valid option")
问题是整个循环只工作一次,但随后它会跳过raw_输入命令并抛出一个值错误。我不明白它为什么会这样做。有什么想法吗

编辑,错误为:

Traceback (most recent call last):
  File "C:\Users\Raffi\Documents\python\encryptor.py", line 37, in <module>
    choice=int(raw_input("press 1 for encrypt, 2 for decrypt"))
ValueError: invalid literal for int() with base 10: ''

根据jme的评论,当您将文本粘贴到终端时,似乎有多个换行符导致您的程序继续为下一个原始输入命令输入任何内容。以下是一系列可能的解决方案:

1使用sys.stdin.read:

但是,然后必须按Ctrl Z/Ctrl D以指示已完成文本输入

2使用Tk.clipboard\u get或类似命令直接从剪贴板读取

from Tkinter import Tk
root = Tk()
root.withdraw()
...
text = raw_input('Please enter plain text or just press enter to copy from the clipboard\n')
if not text:
    text = root.clipboard_get()
3您也可以继续允许输入,直到输入一个空行或其他标记文本结尾的方式

print('Please enter plain text. Enter "stop" when you are finished')
text = ''
while True:
    inp = raw_input()
    if inp == 'stop':
        break
    text += inp

当然,这是假设您要复制的文本中没有写“停止”的行。

请将您遇到的任何错误的全文与您的问题联系起来。哪一行会抛出值错误?解密字符串的作用是什么?您使用的是Python 2还是Python 3?或者您正在使用“从未来导入打印”功能?请注意,在3中,原始输入被替换为输入,而打印是一个函数,而不是一个语句。鉴于此回溯,您似乎在一行中按了两次enter键,第二次击键为Python提供了一行空行输入。@MAttDMo decrypt_string接收一个字符串,编辑它,然后输出一个字符串。
print('Please enter plain text. Enter "stop" when you are finished')
text = ''
while True:
    inp = raw_input()
    if inp == 'stop':
        break
    text += inp