(Python) 在用户定义的函数中尝试和排除
(Python) Try and Except within a User-Defined Function
我做过很多项目(学校项目,我不是太高级),发现在很多程序中我要求用户输入一个整数或小数(浮点数)的值,我需要在 while 循环中使用“try-except”语句,以确保用户输入所需的值类型。
例如:
def main():
userValue = input("Please enter an integer: ")
while(True):
try:
userValue = int(userValue)
break
except:
userValue = input("That is not an integer, try again: ")
print("The integer you entered is: " + str(userValue))
main()
# Input: SADASD (A non-integer value)
# Output: "That is not an integer, try again: "
# Second-Input: 3
# Second-Output: "The integer you entered is: 3"
可以理解的是,在需要用户多次输入的程序中重复输入这整段代码并不是很高效。所以理解,当我需要多次执行一个动作时,用户定义的函数会有所帮助。考虑到这一点,我在 while 循环中使用相同的 try-except 语句定义了自己的函数。但是,现在,当我使用该函数时,它不再打印之前的相同输出,而是打印出用户输入的第一个值。
例如:
def valueCheck_Integer(userInput):
while(True):
try:
userInput= int(userInput)
break
except:
userInput = input("That is not an integer, try again: ")
def main():
userValue = input("Please enter an integer: ")
valueCheck_Integer(userValue)
print("The integer you entered is: " + str(userValue))
main()
# Input: SADASD (A non-integer value)
# Output: "That is not an integer, try again: "
# Second-Input: SASASD
# Second-Output: "That is not an integer, try again: "
# Third-Input: 3
# Third-Output: SADASD (The first value that the user Input, instead of 3)
有人可以向我解释为什么会发生这种情况,以及一些关于如何解决它的建议吗?
谢谢!
这是因为你正在打印 userValue
而不是 userInput
。
我用 return
让它更容易。所以代码会是这样
def valueCheck_Integer(userInput):
while(True):
try:
userInput= int(userInput)
break
except:
userInput = input("That is not an integer, try again: ")
return userInput
def main():
userValue = input("Please enter an integer: ")
print("The integer you entered is: " + str(valueCheck_Integer(userValue)))
main()
您可以像这样缩小代码:
def valueCheck_Integer(userInput):
while not(userInput.isdigit()):
userInput = input("That is not an integer, try again: ")
return userInput
def main():
userValue = input("Please enter an integer: ")
print("The integer you entered is: " + str(valueCheck_Integer(userValue)))
main()
期望函数 get/check/return 整数而不是检查您已有的输入可能会更容易。您可以将用于请求值的字符串传递给它(您也可以传递错误字符串)。它会一直询问直到成功然后return你想要的号码:
def get_integer(question):
while(True):
try:
return int(input(question))
except ValueError:
question = "That is not an integer, try again:"
def main():
userValue = get_integer("Please enter an integer: ")
print("The integer you entered is: " + str(userValue))
main()
首先,问得好。
要了解发生了什么,我们首先必须讨论变量的范围。
当你在函数外定义一个变量时,它就变成了一个叫做全局变量的东西。这基本上意味着您可以从代码中的任何位置访问它。当您在函数中定义变量时,它就成为局部变量。这意味着它只能从您的函数中访问。最后,当一个函数传入一个变量时,它会得到它自己的变量本地副本来使用。
现在让我们看看您的代码。
当您调用 valueCheck_Integer(userInput):
时,该函数会获取自己的 userInput
副本以供使用。因此,函数所做的所有更改都会修改局部 userInput
,而全局 userInput
保持不变。因此,当用户输入正确答案时,全局 userInput
将被打印出来,函数对局部 userInput
所做的更改将丢失。
那么,我们该如何解决这个问题?
主要有两种方法:
1)使用global
关键字
def valueCheck_Integer(userInput):
global userInput
while(True):
try:
userInput= int(userInput)
break
except:
userInput = input("That is not an integer, try again: ")
这个关键字要求函数修改全局userInput
2)返回一个值
def valueCheck_Integer(userInput):
while(True):
try:
userInput= int(userInput)
break
except:
userInput = input("That is not an integer, try again: ")
return userInput
def main():
userValue = input("Please enter an integer: ")
print("The integer you entered is: " + str(valueCheck_Integer(userValue)))
main()
这通过返回 userInput
的本地副本并将全局 userInput
修改为等于本地 userInput
来实现
我使用的第二个代码来自
Osadhi Virochana Jayasinghe Si 的回答。
这是因为,如果您看到打印最终输出的代码行 -:
print("The integer you entered is: " + str(userValue))
你会发现你正在打印的值是你第一次从输入函数中取出的值。但这不是您一直致力于在其他职能中实现的价值。
因此,为了让您更愿意获得该值,函数必须以某种方式 return 返回给您。
为此,您应该允许函数 return 最后一行中的值。
像这样-:
return userInput
然后更改调用函数的行,以便它保存值 returned。
像这样-:
userValue = valueCheck_Integer(userValue)
另外正如其他人提到的那样,您可以使用 global 关键字在全局范围内定义变量。
但这在真正需要之前确实不是一个好的做法,因为它可以增加 var 所占用的 space 的数量,而在变量仅在调用函数时的有限时间内占用 space ,但现在变量在整个程序运行期间占用 space。
虽然 return 不会这样做,因为它只会 return 值,一旦分配给已经定义的变量,它将从 space.[=13 中删除 returned 值=]
这有望解决您的问题。
我希望这有帮助。
也希望您在这场持续的大流行期间平安无事。
我做过很多项目(学校项目,我不是太高级),发现在很多程序中我要求用户输入一个整数或小数(浮点数)的值,我需要在 while 循环中使用“try-except”语句,以确保用户输入所需的值类型。
例如:
def main():
userValue = input("Please enter an integer: ")
while(True):
try:
userValue = int(userValue)
break
except:
userValue = input("That is not an integer, try again: ")
print("The integer you entered is: " + str(userValue))
main()
# Input: SADASD (A non-integer value)
# Output: "That is not an integer, try again: "
# Second-Input: 3
# Second-Output: "The integer you entered is: 3"
可以理解的是,在需要用户多次输入的程序中重复输入这整段代码并不是很高效。所以理解,当我需要多次执行一个动作时,用户定义的函数会有所帮助。考虑到这一点,我在 while 循环中使用相同的 try-except 语句定义了自己的函数。但是,现在,当我使用该函数时,它不再打印之前的相同输出,而是打印出用户输入的第一个值。
例如:
def valueCheck_Integer(userInput):
while(True):
try:
userInput= int(userInput)
break
except:
userInput = input("That is not an integer, try again: ")
def main():
userValue = input("Please enter an integer: ")
valueCheck_Integer(userValue)
print("The integer you entered is: " + str(userValue))
main()
# Input: SADASD (A non-integer value)
# Output: "That is not an integer, try again: "
# Second-Input: SASASD
# Second-Output: "That is not an integer, try again: "
# Third-Input: 3
# Third-Output: SADASD (The first value that the user Input, instead of 3)
有人可以向我解释为什么会发生这种情况,以及一些关于如何解决它的建议吗? 谢谢!
这是因为你正在打印 userValue
而不是 userInput
。
我用 return
让它更容易。所以代码会是这样
def valueCheck_Integer(userInput):
while(True):
try:
userInput= int(userInput)
break
except:
userInput = input("That is not an integer, try again: ")
return userInput
def main():
userValue = input("Please enter an integer: ")
print("The integer you entered is: " + str(valueCheck_Integer(userValue)))
main()
您可以像这样缩小代码:
def valueCheck_Integer(userInput):
while not(userInput.isdigit()):
userInput = input("That is not an integer, try again: ")
return userInput
def main():
userValue = input("Please enter an integer: ")
print("The integer you entered is: " + str(valueCheck_Integer(userValue)))
main()
期望函数 get/check/return 整数而不是检查您已有的输入可能会更容易。您可以将用于请求值的字符串传递给它(您也可以传递错误字符串)。它会一直询问直到成功然后return你想要的号码:
def get_integer(question):
while(True):
try:
return int(input(question))
except ValueError:
question = "That is not an integer, try again:"
def main():
userValue = get_integer("Please enter an integer: ")
print("The integer you entered is: " + str(userValue))
main()
首先,问得好。 要了解发生了什么,我们首先必须讨论变量的范围。
当你在函数外定义一个变量时,它就变成了一个叫做全局变量的东西。这基本上意味着您可以从代码中的任何位置访问它。当您在函数中定义变量时,它就成为局部变量。这意味着它只能从您的函数中访问。最后,当一个函数传入一个变量时,它会得到它自己的变量本地副本来使用。
现在让我们看看您的代码。
当您调用 valueCheck_Integer(userInput):
时,该函数会获取自己的 userInput
副本以供使用。因此,函数所做的所有更改都会修改局部 userInput
,而全局 userInput
保持不变。因此,当用户输入正确答案时,全局 userInput
将被打印出来,函数对局部 userInput
所做的更改将丢失。
那么,我们该如何解决这个问题?
主要有两种方法:
1)使用global
关键字
def valueCheck_Integer(userInput):
global userInput
while(True):
try:
userInput= int(userInput)
break
except:
userInput = input("That is not an integer, try again: ")
这个关键字要求函数修改全局userInput
2)返回一个值
def valueCheck_Integer(userInput):
while(True):
try:
userInput= int(userInput)
break
except:
userInput = input("That is not an integer, try again: ")
return userInput
def main():
userValue = input("Please enter an integer: ")
print("The integer you entered is: " + str(valueCheck_Integer(userValue)))
main()
这通过返回 userInput
的本地副本并将全局 userInput
修改为等于本地 userInput
我使用的第二个代码来自 Osadhi Virochana Jayasinghe Si 的回答。
这是因为,如果您看到打印最终输出的代码行 -:
print("The integer you entered is: " + str(userValue))
你会发现你正在打印的值是你第一次从输入函数中取出的值。但这不是您一直致力于在其他职能中实现的价值。
因此,为了让您更愿意获得该值,函数必须以某种方式 return 返回给您。
为此,您应该允许函数 return 最后一行中的值。 像这样-:
return userInput
然后更改调用函数的行,以便它保存值 returned。
像这样-:
userValue = valueCheck_Integer(userValue)
另外正如其他人提到的那样,您可以使用 global 关键字在全局范围内定义变量。 但这在真正需要之前确实不是一个好的做法,因为它可以增加 var 所占用的 space 的数量,而在变量仅在调用函数时的有限时间内占用 space ,但现在变量在整个程序运行期间占用 space。 虽然 return 不会这样做,因为它只会 return 值,一旦分配给已经定义的变量,它将从 space.[=13 中删除 returned 值=]
这有望解决您的问题。 我希望这有帮助。 也希望您在这场持续的大流行期间平安无事。