英文:
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("Enter the number between 5 to 9:")
if (int(inp)<=5 or int(inp)>=9):
raise ValueError ("Number should be between 5 to 9")
elif inp=="Quit":
print("You Quit")
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("Enter any value between 5 and 9 : ")
if inp == "Quit":
print("You Quit")
elif (int(inp) <= 5 or int(inp) >=9):
raise ValueError("Value should be between 5 and 9")
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")
转换为整数值。
希望这解释清楚了!我们都是从某个地方开始的,祝你好运
英文:
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 == 'Quit':
print("You Quit")
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("0")
will result in an integer value of 0
, but there's no way for the interpreter to convert int("Quit")
into an integer value.
Hope that explains it! We all started somewhere, good luck
答案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)<=5 or int(inp)>=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)<=5 or int(inp)>=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("Enter the number between 5 to 9:")
if inp == "Quit":
print("Quit")
else:
try:
inp=int(inp)
if inp<5 or inp>9:
print("Number should be between 5 to 9")
else:
print(f"Number input is: {inp}")
except:
print("Input should be an integer.")
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.)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论