[英]Proper Validation Loops with Spyder 4.1.4 and python 3.8
所以我的代码是这样的:
def main():
print("Press '1' to start a new dice roll calculation")
print("or press '0' to end the program") #escape key
validation = int(input()) #so the user can press enter
while validation != 1 or validation != 0: #input validation loop with escape key
print("Press ENTER to start a new dice roll calculation")
validation = int(input())
当我出于某种原因尝试运行验证循环时,它会让我陷入无限循环,但是当我检查我的教科书(Tony Gaddis,2017 年)时,它应该是正确的。 我哪里错了?
它正在检查 1 或 0 是否不存在。 如果输入 1,则 0 不存在,反之亦然,因此它总是会导致错误。 将or
更改为and
如下所示:
def main():
print("Press '1' to start a new dice roll calculation")
print("or press '0' to end the program") #escape key
validation = int(input()) #so the user can press enter
#input validation loop with escape key using AND instead of OR
while validation != 1 and validation != 0:
print("Press ENTER to start a new dice roll calculation")
validation = int(input())
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.