2017-04-15 111 views
0

输出应该以这种格式显示时间:上午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() 
+0

你期待什么输入'cse_time'?像“0833”? – kabanus

+0

试图创建一个需要一个数字并显示此格式的输入:8:00 –

+2

首先修复缩进,缩进问题无法回答 – abccd

回答

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

它看起来像可怜的缩进,OP可能在while循环下缩进了所有的东西,所以可以通过改变'again'变量退出while循环。这不是问题 – abccd

0

如果cse_time输入是在“0800”是“8:00”的格式,那么你就可以str(cse_time)[:2]+":"+str(cse_time)[2:] + ' a.m.'取代str(cse_time) + ' a.m.'以分号补充。或者你可能有cse_time是一个字符串,所以输入可以是8:00