我的代码应输出:
Encipher
Enter 1 to encipher or 2 to decipher: 1
Enter text you wish to encipher: My dog has fleas.
Enter the number of characters to shift: 7
The encrypted text is: Fr whz atl yextl.
Decipher
Enter 1 to encipher or 2 to decipher: 2
Enter text you wish to decipher: Fr whz atl yextl.
The most likely shift is: 7
My dog has fleas.
但它不起作用,我不明白如何使用try / except使我的代码执行此操作。
while True:
try:
num = int(raw_input('Enter 1 or 2:'))
break
except ValueError:
print "You have to enter 1 or 2, try again"
if (num == 1):
num = int(raw_input('Enter a number:'))
num = int(raw_input('encipher'))
print "Enter text to encipher"
print "Enter the number of characters you want to shift"
elif (num == 2):
num = int(raw_input('Enter a number:'))
num = int(raw_input('decipher'))
print "Enter text to decipher"
print "Enter the number of characters you want to shift"
答案 0 :(得分:0)
您的直接问题是,除了输入的数字与您打印的文字不匹配外,您不会得到。 Python不会自动读取和解释您发送到屏幕的消息。相反,在顶部尝试这一点:进入一个坚持法律数据的无限循环。继续前进,直到获得合法数据。抓住每个号码,检查一下是否合法;如果符合您的需要,请退出循环。
while True:
while True:
num = int(raw_input('Enter 1 or 2:'))
if num in [1,2]:
break
print "You have to enter 1 or 2, try again"
if (num == 1):
...
你做得很好,只需编写几行代码,然后再继续工作。请注意,较低的代码中存在一些概念性问题。对于isntance,您要求用户输入要加密的文本,但之后您尝试将其转换为整数。 那会让你得到ValueError。
如果再次卡住,请发布一个新问题。