python - 尝试使用 try / except 函数更改多个变量
问题描述
def tryexcept(variable,range1,range2):
while variable not in range(range1,range2):
try:
if variable not in range(range1,range2):
variable = int(input("Enter your decision \033[95m(1)\033[0m or \033[95m(2)\033[0m"))
# Making the options stand out with \033[95m for error prevention, plus it looks nice
except:
print("Not valid please enter an intiger \033[95m(1 or 2)\033[0m")
menu_option = 0
tryexcept(menu_option,1,3)
我尝试了全局变量并更改了变量的定义顺序
解决方案
这是你想要的吗?
def tryexcept(variable,range1,range2):
while variable not in range(range1,range2):
try:
if variable not in range(range1,range2):
variable = int(input("Enter your decision \033[95m(1)\033[0m or \033[95m(2)\033[0m"))
# Making the options stand out with \033[95m for error prevention, plus it looks nice
except:
print("Not valid please enter an intiger \033[95m(1 or 2)\033[0m")
# Returns the variable once the loop is finished
return variable
menu_option = 0
# Sets menu_option to the return value
menu_option = tryexcept(menu_option,1,3)
推荐阅读
- javascript - 如何在 html 中找到用于数据验证的正则表达式匹配?
- dialogflow-es - 如何导出代理Dialogflow的特定环境
- c# - 如何留下孤立数据?
- node.js - 字典 Node.js 中的无法定位值
- laravel - Laravel注册用户时如何设置Remember Token?
- amazon-dynamodb - AWS DynamoDB 扫描
- php - 如何创建简码函数来在页面中运行脚本?
- paypal - 无法使用个人帐户为我的 PayPal 创建沙盒/测试帐户
- python - 如何从字典中搜索列表中的特定字符串并在mongodb中返回包含该字符串的所有元素?
- mysql - 如何在 Mysql Query 中排除特定行