Python 如何在输出中显示冒号以显示时间?
输出应以以下格式显示时间:上午8:00Python 如何在输出中显示冒号以显示时间?,python,string,integer,output,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:')) c
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()
看起来你需要一种退出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()
如果
cse_time
的输入格式为“0800”为“8:00”,则可以将str(cse_time)+“a.m.”
替换为str(cse_time)[:2]+“:”+str(cse_time)[2::+'a.m.
以添加分号。或者您可以将cse\u时间
设置为字符串,这样输入可以是8:00
对于cse\u时间
,您希望输入什么?类似于“0833”的内容?尝试创建一个接受数字的输入并显示此格式:8:00首先修复缩进,如果所有数字都产生8:00
,缩进不好的问题无法回答?如果我输入850
?@vishes\u shell如何确定while循环下的缩进是正确的?OP是这么说的吗?如果你不是100%确定,不要编辑问题。接下来的三行也可能在while循环下。Wait for OPIt看起来缩进不良,OP可能缩进了while循环下的所有内容,因此可以退出while循环,因为再次更改
变量。这不是问题所在