简体   繁体   English

数据验证-Python 2.7

[英]Data Validation - Python 2.7

This is the code that I have edited so far for the upcoming AS computing Preliminary code. 到目前为止,这是我为即将到来的AS计算初步代码编辑的代码。 It's homework and I am stuck as to why it does not work. 这是家庭作业,我对为什么不起作用感到困惑。 Any suggestions? 有什么建议么? Thanks 谢谢

def GetKeyForCaesarCipher():  
  while True:
    key = int(raw_input('Enter the amount that shifts the plaintext alphabet to the ciphertext alphabet: '))
    try:
      i = int(key)
      break
    except ValueError:
      print 'please enter an interger'
    return (key)

you should just do key = raw_input(...) rather than key = int(raw_input(...)) 你应该只做key = raw_input(...)而不是key = int(raw_input(...))

With the former, key is a string until you try to make it an integer. 对于前者, key是一个字符串,直到try将其设为整数为止。 With the latter, you attempt to construct an int from it right away without guarding that attempt with any exception handling. 对于后者,您尝试立即从其构造一个int ,而不用任何异常处理来保护该尝试。


There are some other logic flaws as well. 还有一些其他逻辑缺陷。 Right now, your infinite while loop doesn't do anything for example. 现在,您的无限while循环不会执行任何操作。 (you return after the first pass no matter what). (无论如何,您都会在第一遍之后返回)。 Moving the return statment where the break is would probably* make it do what you want. return语句移动到break处可能会*使它执行所需的操作。 Also, you don't do anything with the int that you construct ( i ), although it's unclear whether or not you should be returning it or key . 此外,你不跟任何int您构建( i ),但目前还不清楚是否应该被返回,或key

*I'm making assumptions about what your program is supposed to do here... *我正在假设您的程序应该在这里做什么...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM