简体   繁体   English

如何使elif语句和ValueError除外重复function?

[英]How to make elif statement and except ValueError repeat function?

I am on section 4 of the Udemy "Automate the Boring Stuff" course, which covers handling errors with try/except.我在 Udemy“自动化无聊的东西”课程的第 4 部分,其中涵盖了使用 try/except 处理错误。

How can I get the script to run from the beginning if the user input is an invalid non-integer or negative integer?如果用户输入是无效的非整数或负数 integer,如何让脚本从头开始运行?

print('How many cats do you have?')
numCat = input()
try:
    while True:
        if int(numCat) >= 4:
            print('That is a lot of kitties')
            break
        elif int(numCat) <= -1:
            print('You can not have '+str(numCat)+' kitties, try again')
            break
        else:
            print('That is not that many kitties')
            break
except ValueError:
    print('Please enter a number, try again')

What's the purpose of the loop if every path has a break in it?如果每条路径都有break ,那么循环的目的是什么? You could do something like this:你可以这样做:

cond = True
while cond:
    print('How many cats do you have?')
    numCat = input()
    try:
        cond = False
        if int(numCat) >= 4:
            print('That is a lot of kitties')
        elif int(numCat) <= -1:
            print('You can not have '+str(numCat)+' kitties, try again')
        else:
            print('That is not that many kitties')
    except ValueError:
        print('Please enter a number, try again')

Or either with break或者要么break

while True:
    print('How many cats do you have?')
    numCat = input()
    try:
        if int(numCat) >= 4:
            print('That is a lot of kitties')
        elif int(numCat) <= -1:
            print('You can not have '+str(numCat)+' kitties, try again')
        else:
            print('That is not that many kitties')
        break
    except ValueError:
        print('Please enter a number, try again')

I would personally do something like this:我个人会做这样的事情:

BONUS: isdigit only counts positive numbers!奖励: isdigit只计算正数!

numCat = None
while True:
  numCat = input()
  if numCat.isdigit():
     break
  print('Please enter a positive number.')
if int(numCat) >= 4:
  print('That is a lot of kitties')
else:
  print('That is not that many kitties')

Not sure if this is the most efficient solution or if there are better ones.不确定这是否是最有效的解决方案,或者是否有更好的解决方案。 But what I would do is loop through the try and let the user input again但我要做的是循环尝试并让用户再次输入

print('How many cats do you have?')
numCat = input()
loopCount = 0
while loopCount == 0:
    numCat = input()
    try:
        while True:
            if int(numCat) >= 4:
                print('That is a lot of kitties')
                break
            elif int(numCat) <= -1:
                print('You can not have '+str(numCat)+' kitties, try again')
                break
            else:
                print('That is not that many kitties')
                break
        loopCount = 1
    except ValueError:
        print('Please enter a number, try again')

move your try-except block and input inside while loop to catch invalid inputs.移动您的try-except块并在while循环内input以捕获无效输入。

while True:
    print('How many cats do you have?')
    numCat = input()
    try:
        if int(numCat) >= 4:
            print('That is a lot of kitties')
            break
        elif int(numCat) <= -1:
            print('You can not have ' + str(numCat) + ' kitties, try again')
            break
        else:
            print('That is not that many kitties')
            break
    except ValueError:
        print('Please enter a number, try again')

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

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