如何在输出中显示冒号以显示时间?

时间:2017-04-15 16:03:14

标签: python string integer output

输出应以此格式显示时间:上午8:00

def save_time():
     cse_info = {}
     again = 'Y'     
     while again.lower() == 'y':

             cse_num = int(input('Enter course number:'))
             cse_time = int(input('Enter course meeting time:'))

             cse_info['CS' + str(cse_num)] = str(cse_time) + ' a.m.'         
             again = input('Do you want to enter another course?')        
     print(cse_info)

save_time()

2 个答案:

答案 0 :(得分:0)

看起来你需要一种方法来退出你的while循环...它应该可以工作。

def save_time():

         cse_info = {}
         again = 'Y'

         while again.lower() == 'y':

             cse_num = int(input('Enter course number:'))
             cse_time = int(input('Enter course meeting time:'))
             break # maybe you want to check for an exit condition?

         cse_info['CS' + str(cse_num)] = str(cse_time) + ' a.m.'

         again = input('Do you want to enter another course?')

         print(cse_info)


save_time()

这次我会为你做这件事,但这不是一个家庭作业网站。

def save_time():
         cse_info = {}
         while True:
             cse_num = int(input('Enter course number:'))
             cse_time = int(input('Enter course meeting time:'))
             cse_info['CS' + str(cse_num)] = str(cse_time) + ' a.m.'
             again = input('Do you want to enter another course? (1=yes 2=no')
             if not again: break

         print(cse_info)

save_time()

答案 1 :(得分:0)

如果cse_time的输入格式为" 0800"在" 8:00",然后您可以用str(cse_time) + ' a.m.'替换str(cse_time)[:2]+":"+str(cse_time)[2:] + ' a.m.'以添加分号。或者您可以cse_time为字符串,因此输入可以是8:00