![](/img/trans.png)
[英]Figuring out if all data in a Panda dataframe row is the same except for a particular column
[英]Figuring out except ValueError
跟随 Al Sweigarts python 课程并尝试修改他的 cat 代码一些。 我可以只使用 if 和 elif 语句输入“除了 ValueError”,但我认为使用 while 语句我搞砸了。 我希望这个简单的代码在用户输入错误的东西时重复,这是目前有效的。 我只需要输入一些解决非整数的东西作为输入。
这与没有使用 break/continue 语句有关吗?
print('How many cats do you got')
numCats = int(input())
while numCats < 0:
print('That is not a valid number')
print('How many cats do you got')
numCats = int(input())
if numCats >= 4:
print('That is a lot of cats')
elif numCats < 4:
print('That is not a lot of cats')
except ValueError:
print('That was not a valid number')
如果输入无效数字,我希望代码重复,同时在非整数值后重复。 不过,我无法通过除了 ValueError 部分。 谢谢!
一个except
块需要一个try
块。 在try
块中,您会发现异常,如果发现,则运行except
子句。
while True:
try:
print('How many cats do you got: ')
numCats = int(input())
if numCats >= 0:
break
else:
print('That was not a valid number')
except ValueError:
print('That was not a valid number')
if numCats >= 4:
print('That is a lot of cats')
elif numCats < 4:
print('That is not a lot of cats')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.