我想知道我应该为这个def函数使用什么函数,以便用户只能输入字符串而不是整数 -
def GetTextFromUser():
TextFromUser = raw_input('Please enter the text to use: ')
return TextFromUser
Run Code Online (Sandbox Code Playgroud) 我需要Python 2.7中的数据验证问题的帮助,它做我想要的不接受字符串,但它不接受整数,因为它应该做.
def GetKeyForCaesarCipher():
while True:
key =(raw_input('Enter the amount that shifts the plaintext alphabet to the ciphertext alphabet: '))
try:
i=int(key)
break
except ValueError:
print ('Error, please enter an integer')
return key
Run Code Online (Sandbox Code Playgroud) 我已添加此功能,以确保用户实际上想要退出该程序.当你真的想要退出时它可以工作但是如果你想返回程序它只是循环语句:
def WantToQuit():
Quit = raw_input("Please enter y if you are sure you want to quit, if not press n to return ")
if Quit == 'y':
print ('')
elif Quit == 'n':
DisplayMenu()
return WantToQuit()
Run Code Online (Sandbox Code Playgroud)
别处:
elif Choice == 'q':
WantToQuit()
raw_input('Press enter key to continue ')
Run Code Online (Sandbox Code Playgroud) 因此,对于考试问题,我遵循了这个特定的伪代码,它基本上是一个程序,它使用与ceasar密码相同的原理加密数字序列.它应该工作但由于某种原因它返回错误.
TypeError: 'int' object is not iterable
Run Code Online (Sandbox Code Playgroud)
下面是代码,我希望你们能帮助我,非常感谢
plainNum = input("enter a number to encode ")
codedNum = ' '
Key = input("enter a key ")
for i in plainNum:
codedNum = codedNum + str((int(i)+key)%10)
print codedNum
Run Code Online (Sandbox Code Playgroud)