代码错误 - typeerror,indexerror,valueerror

时间:2014-11-12 19:28:15

标签: python

def time():
    timeString = input("Enter time in military format(hh:mm):")
    enteries = timeString.split(":")
    hours = int(enteries[0]) 
    minutes = int(enteries[1]) <---IndexError: list index out of range

    if enteries <= 1200:      <-------errorTypeError: unorderable types: str() <= int()
        print(hours,":", "AM")
    else:
        time > 1200 
        standard = (time - 1200)
        print (standard,"PM")       

time()
  

以军事格式输入时间(hh:mm):09:87

Traceback (most recent call last):
  File "E:\ICS 140\python\ass5.py", line 17, in <module>
    time()
  File "E:\ICS 140\python\ass5.py", line 9, in time
    if int(timeString) <= 1200:
ValueError: invalid literal for int() with base 10: '09:87'
  

以军事格式输入时间(hh:mm):09:87

Traceback (most recent call last):
  File "E:\ICS 140\python\ass5.py", line 17, in <module>
    time()
  File "E:\ICS 140\python\ass5.py", line 7, in time
    minutes = int(enteries[2])
IndexError: list index out of range
  

以军事格式输入时间(hh:mm):09:87

Traceback (most recent call last):
  File "E:\ICS 140\python\ass5.py", line 17, in <module>
    time()
  File "E:\ICS 140\python\ass5.py", line 9, in time
    if int(timeString <= 1200):
TypeError: unorderable types: str() <= int()

1 个答案:

答案 0 :(得分:0)

你的代码有一些问题,这就是我认为你拍摄的内容

def time():
    timeString = input('Enter time in military format (hh:mm): ')
    hours, minutes = map(int, timeString.split(':'))
    if hours <= 12:
        return '{}:{} AM'.format(hours, minutes)
    else:
        standard = hours - 12
        return '{}:{} PM'.format(standard,minutes)

测试

>>> time()
Enter time in military format (hh:mm): 09:15
'9:15 AM'

>>> time()
Enter time in military format (hh:mm): 21:25
'9:25 PM'