【问题标题】:Trying to change multiple variables with try / except function尝试使用 try / except 函数更改多个变量
【发布时间】:2021-05-01 02:58:25
【问题描述】:
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)

我尝试了全局变量并更改了变量的定义顺序

【问题讨论】:

  • 你的函数返回什么?

标签: python exception while-loop global-variables try-catch


【解决方案1】:

这是你想要的吗?

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)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-11-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多