输入负数时,如何向用户打印消息?
我正在学习尝试
和现在,
语句现在,我希望该程序在插入负数时打印出一条消息,但我不知道如何创建该输出的语句顺序:
print("how many dogs do you have?")
numDogs= input()
try:
if int(numDogs)>=4:
print("that is a lof of dogs")
else:
print("that is not that many dogs")
except:
print("you did not enter a number")
我希望该程序在用户输入负数时打印TJE输出。 我该怎么做?
I am learning about the try
and except
statements now and I want the program to print out a message when a negative number is inserted but I don't know how to create the order of the statements for this output:
print("how many dogs do you have?")
numDogs= input()
try:
if int(numDogs)>=4:
print("that is a lof of dogs")
else:
print("that is not that many dogs")
except:
print("you did not enter a number")
I want the program to print tje output when the user enters a negative number.
How can I do it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
只是代码就像
尝试:
如果没有< 0:
打印(“否是负面”),
而进一步的代码将是
Just code is like
try:
if no<0:
print("no is negative ")
And further code will be as it is
这是您要实现的目标。
您可以首先创建自定义异常。参见 https://www.programiz.com/python-programiz.com/python-programming/python-programming/user-programming/user-defined - 外观
之后,您应该有一个条件以首先检测负值,然后从中引起错误。
最后,您可以创建多个异常以获取不同的错误。第一个因负值输入(就像您的状况对您所做的那样)提出了第一个,而第二个则用于捕获非有效值(例如字符)。
请注意,您的
int(输入)
段应在您的尝试块中以检测错误Here is what you are trying to achieve.
You can start by creating a custom exception. See https://www.programiz.com/python-programming/user-defined-exception
After which, you should have a condition to first detect the negative value then raise an error from it.
Finally, You can then create multiple excepts to get different error. The first except is raised on input of negative values (as your condition will do for you) whereas the second is used to catch non valid value such as characters.
Note that your
int(input)
segment should be WITHIN your try block to detect the error我发现的解决方案来自这个问题
您可以像 @Jackson的答案那样提出一个值,或者您可以在下面的代码中提出例外。
我应该指出的一件事是,只要检查这个数字是否是&lt,就可以更好地练习。 0或NAN。但是,就像您说的那样,您正在尝试了解尝试和语句以外的尝试。 很酷
我认为在此代码段中,您可以丢下想要的错误,并且尝试会捕获它 。猜猜我也在学习很多。只有一部分是,如果发生这种情况,如果您的代码不知道它是一个负数问题,或者数字是否类似于“ A”,它不能转换为一个(以此方式)。
总而言之,我认为最好解决此问题,没有尝试 /除了语句以外:
Notice that I wrapped everything in a `main` function, because that way we can return a string with the result; this is important because if we get an error we want to break out of the function, which is what return does.
Solution I found is from this SO question
You can raise a valueError like @Jackson's answer, or you can raise an Exception like in my code below.
One thing that I though I should point out is that its probably better practice to just check if the number is < 0 or is NAN. But like you said, you're trying to learn about the try and except statements. I thought it was cool that in this code segment:
you can throw what error you want and your try will catch it. Guess im still learning a lot too. Only part is, if that happens if your code then one won't know if its a negative number issue or if the number is something like "a", which cannot be converted to one (in this way).
All in all, I think its best to solve this problem with no try / except statements:
Notice that I wrapped everything in a `main` function, because that way we can return a string with the result; this is important because if we get an error we want to break out of the function, which is what return does.