Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ruby-on-rails-3/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 3.x 建议一种更好的方法来处理异常,以防_Python 3.x_Exception Handling - Fatal编程技术网

Python 3.x 建议一种更好的方法来处理异常,以防

Python 3.x 建议一种更好的方法来处理异常,以防,python-3.x,exception-handling,Python 3.x,Exception Handling,我正在做一个课堂作业,我们把军事时间转换成标准时间,我想我最好有单独的函数来获取输入和检查输入——我的推理是,如果check_输入函数失败,我可以继续循环get_输入函数,直到用户以正确的方式输入它格式 然而,当我像“jklfd”一样输入jibberish时,我的get_输入函数崩溃了,因为它无法将它转换为作为函数一部分的列表 在这种情况下,有没有更好的方法来处理异常?此外,任何一般性的提示或建议都是非常感谢的。提前感谢您的帮助 __author__ = 'Ethan' #This pr

我正在做一个课堂作业,我们把军事时间转换成标准时间,我想我最好有单独的函数来获取输入和检查输入——我的推理是,如果check_输入函数失败,我可以继续循环get_输入函数,直到用户以正确的方式输入它格式

然而,当我像“jklfd”一样输入jibberish时,我的get_输入函数崩溃了,因为它无法将它转换为作为函数一部分的列表

在这种情况下,有没有更好的方法来处理异常?此外,任何一般性的提示或建议都是非常感谢的。提前感谢您的帮助

    __author__ = 'Ethan'
#This program takes input in military time in the format hour:minutes and outputs
#the time in standard time hour:minute AM/PM

def main():
    print_intro()
    while True:
        mil_h, m = get_inputs()
        if check_input(mil_h,m):
            break
    reformat_time(mil_h,m)

def print_intro():
    print("This program takes input as military time from user")
    print("in format hour:minute ex. 23:34")
    print("and outputs the time in standard AM/PM format")
    print("ex. from above 11:34 PM")

def get_inputs():
    raw = input("Enter time: ")
    time_list = raw.split(":")
    mil_h = int(time_list[0])
    m = int(time_list[1])
    return mil_h, m

def check_input(mil_h,m):
    try:
        if mil_h >= 24 or mil_h <0:
            print("Time must be in format hh:mm")
            print("Hour must be in range 0 to 23")
            return False
        elif m >= 60 or m <0:
            print("Time must be in format hh:mm")
            print("Minute must be in range 0 to 59")
            return False
        else:
            return True
    except:
        print("Input must be in military time in format hh:mm")

def reformat_time(mil_h,m):
    am_pm = "AM"
    if mil_h == 12:
        am_pm = "PM"
        stand_h = 12
    elif mil_h > 12:
        am_pm = "PM"
        stand_h = mil_h % 12
    else:
        stand_h = mil_h
    print(stand_h,':', m,' ', am_pm, sep='')

main()
\uuuu作者\uuuuu='Ethan'
#该程序以小时:分钟和输出的格式输入军事时间
#以标准时间小时表示的时间:分钟AM/PM
def main():
打印介绍()
尽管如此:
mil_h,m=获取输入()
如果检查输入(mil_h,m):
打破
重新格式化时间(mil_h,m)
def print_intro():
打印(“此程序将用户输入的军事时间作为军事时间”)
打印(“格式为小时:分钟,例如23:34”)
打印(“并以标准AM/PM格式输出时间”)
打印(“例如,从晚上11:34以上开始”)
def get_输入()
原始=输入(“输入时间:”)
时间列表=原始分割(“:”)
mil_h=int(时间列表[0])
m=int(时间列表[1])
返回mil_h,m
def检查输入(密耳高度,米):
尝试:
如果密耳高度>=24或密耳高度=60或m 12:
am_pm=“pm”
支架高度=密耳高度%12
其他:
支架高度=密耳高度
印刷品(展位号:'、m'、上午/下午、九月=')
main()

在拆分字符串之前,请使用if语句

if(raw.contains(":")&& raw.length()>=3 && raw) {
list=raw.split(":")
//rest of code
} else { throw exception}
应编辑异常的代码,使其如下所示:

except:
println("Code must be in military format")
get_inputs()
//recall the check_input method for the values found in get_inputs
check_input()

这样,您就知道raw的格式是正确的,您可能需要为raw添加更多的先决条件(比如确保它只包含数字),这样程序就不会因不需要的输入而崩溃。(很抱歉,我不太懂python)

谢谢您的回复。如果我这样做,如果用户的输入未通过测试,我如何返回并再次要求用户输入?使用while循环和布尔变量跟踪成功