Python代码在执行时出现错误。

huangapple go评论69阅读模式
英文:

Python code getting error while execution

问题

Code1中的问题是在if语句中的条件判断。以下是修正后的代码:

inp = input("请输入一个介于5到9之间的数字:")

if not inp.isdigit():
    raise ValueError("输入应为数字")

num = int(inp)
if num < 5 or num > 9:
    raise ValueError("数字应在5到9之间")
elif inp == "Quit":
    print("你退出了")

Code1中的问题是条件判断的顺序,首先应该检查输入是否是数字,然后再检查数字是否在5到9之间。这样可以避免在输入"Quit"时引发错误。修正后的代码先检查是否为数字,然后再进行数字范围的检查。

英文:

I am learning python as a beginner, and am executing a piece of code below:

Code1:

inp=input(&quot;Enter the number between 5 to 9:&quot;)

if (int(inp)&lt;=5 or int(inp)&gt;=9):

   raise ValueError (&quot;Number should be between 5 to 9&quot;)

elif inp==&quot;Quit&quot;:
  print(&quot;You Quit&quot;)

But I am getting error running above code when I enter "Quit" as an input. While the below code is running fine:

Code 2:

inp = input(&quot;Enter any value between 5 and 9 : &quot;)

if inp == &quot;Quit&quot;:

    print(&quot;You Quit&quot;)

elif (int(inp) &lt;= 5 or int(inp) &gt;=9):

    raise ValueError(&quot;Value should be between 5 and 9&quot;)

I would be highly obliged if you can make me understand of the error in the Code1.

Thank you!!!!

I tried a python code and am getting an error. I am expecting if someone can make me understand my mistake. in the code.

答案1

得分: 3

在第一个if语句中,你试图将"Quit"强制转换为整数。你可以通过将elif部分放在if之前,将需要数字的条件放在随后的条件中来克服这个问题。

示例:

if inp == 'Quit':
    print("You Quit")
elif ......:
  # Other stuff

问题出现在Python尝试将"Quit"转换为整数时,而你却使用了int(inp),但inp的值是"Quit"。

int("0")将得到整数值0,但解释器无法将int("Quit")转换为整数值。

希望这解释清楚了!我们都是从某个地方开始的,祝你好运 Python代码在执行时出现错误。

英文:

In the first if statement, you are trying to cast "Quit" into an integer. You can overcome this by simply putting the elif part as the if, and the conditions that require numbers, to be the subsequent conditions.

Example:

if inp == &#39;Quit&#39;:
    print(&quot;You Quit&quot;)
elif ......:
  # Other stuff

The problem occurs because Python is trying to convert "Quit" into an integer when you have int(inp) but the value of inp is "Quit"

int(&quot;0&quot;) will result in an integer value of 0, but there's no way for the interpreter to convert int(&quot;Quit&quot;) into an integer value.

Hope that explains it! We all started somewhere, good luck Python代码在执行时出现错误。

答案2

得分: 0

因为当您输入'Quit'时,它是一个字符串值。所以

if (int(inp)<=5 or int(inp)>=9):

会导致错误,因为程序无法将值'Quit'处理为整数。我唯一能想到的解决方法是使用第二段代码,因为在那里

if (int(inp)<=5 or int(inp)>=9):

在程序检查输入是否为'Quit'之后进行处理。

英文:

It's because when you enter 'Quit', it's a string value. So the

if (int(inp)&lt;=5 or int(inp)&gt;=9):

gives you an error as the program can't process the value 'Quit' as an integer.
The only solution I can think of is to use the 2nd code as here the

if (int(inp)&lt;=5 or int(inp)&gt;=9):

is processed after the program has checked if the input is 'Quit'

答案3

得分: 0

正如krish之前提到的,错误发生在执行int(inp)时,因为默认情况下每个输入都是字符串。

您可以使用下面的新代码:

inp = input("输入5到9之间的数字:")

if inp == "Quit":
    print("退出")
else:
    try:
        inp = int(inp)
        if inp < 5 or inp > 9:
            print("数字应该在5到9之间")
        else:
            print(f"输入的数字是:{inp}")
    except:
        print("输入应该是一个整数。")

希望以上内容对krish的解释有所帮助
顺便说一下希望您也注意到了我对您的操作符进行的更正如果您的意图是包括5和9的话)。
英文:

As mentioned previously by krish, the error happens when int(inp) is executed as every input is a string by default.

You may use the new code below:

inp=input(&quot;Enter the number between 5 to 9:&quot;)

if inp == &quot;Quit&quot;:
    print(&quot;Quit&quot;)
else:
    try:
        inp=int(inp)
        if inp&lt;5 or inp&gt;9:
            print(&quot;Number should be between 5 to 9&quot;)
        else:
            print(f&quot;Number input is: {inp}&quot;)
    except:
        print(&quot;Input should be an integer.&quot;)

Hope the above helps in addition to the explanation by krish.
Btw, hope you noticed my correction on your operators as well (if it was your intention to include 5 and 9.)

huangapple
  • 本文由 发表于 2023年6月5日 15:08:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/76404176.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定