【发布时间】:2014-12-12 21:56:26
【问题描述】:
我正在编写一些代码,但我遇到了问题。我编写的函数工作正常,但主循环(应该无限工作)只能正常工作一次。这是代码:
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_Input 命令并引发值错误。我不明白为什么它会这样做。有什么想法吗?
编辑,错误:
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: ''
【问题讨论】:
-
请edit 提供您遇到的任何错误的全文。
-
哪一行会抛出值错误?
decrypt_string()是做什么的? -
您使用的是 Python 2 还是 Python 3?还是您使用
from __future__ import print_function? (注意,在 3 中,raw_input被替换为input,print是一个函数,而不是一个语句。) -
鉴于该回溯,看起来您连续按了两次 Enter,第二次击键为 Python 提供了一个空输入行。
-
@MAttDMo decrypt_string 接受一个字符串,编辑它,然后输出一个字符串。
标签: python