+ 4
Python 3 question
Hi all, I need to write a program which takes an integer as its input, representing the time using the 24-hour clock. For example, 740 is 7.40 am; 2235 is 10.35 pm. Midnight is zero. Also the program respond with a ' ?' if the time represented by the number is impossible, such as 2400, -15 or 1363. The program should be created with basic functions such as if/elif,while. thanks in advance.
6 odpowiedzi
+ 5
+ 4
Sounds like homework. If you're learning Python, then at least attempt the homework. Anyway, show that you've attempted to solve the problem yourself before asking (in general).
https://www.sololearn.com/discuss/1021621/?ref=app
+ 4
Good job! :D
List slicing is really useful, by the way, and it avoids things you may have to do in other languages.
+ 3
Have you tried using the time import?
+ 3
possible, but as I said it should be solved ina simple way, for beginners without using any modules. :)
+ 2
Hi all, yes it is homework, i guess you are right that you shouldn't be doing my homework but I have tried myself first and I have failed to do it. Anyway, I do appreciate all of your help. Here is what I have done:
a = 0
h = 0
m = 0
finished = False
while not finished:
print("Please enter the time 24-hour clock: ", end ="")
s = input()
a = int(s)
h = int(str(s[0,2]))
m = int(str(s[2,4]))
if a == 0:
print("It's midnight, start of the day, 12:00.")
if m<0 or m>=60:
print("?")
if h<=12:
print("Good morning!")
print("12-hour clock: Time is: " + h + ":" + m + "a.m.")
else:
h = h - 12
print("12-hour clock: Time is: " + h + ":" + m + "p.m.")
Unfortunately i am getting an error message: TypeError: string indices must be integers? Could you please advise what's wrong with my syntax and how to fix it?
EDIT:
Right, I think it's working now:
a = 0
h = 0
m = 0
b = 0
c = 0
finished = False
while not finished:
print("\n")
print("Please enter the time 24-hour clock: ", end ="")
s = input()
b = str(s[0:2])
c = str(s[2:4])
h = int(b)
m = int(c)
if m<0 or m>=60:
print("?")
else:
if h+m == 0:
print("It's midnight, start of the day, 12:00.")
else:
if h<=12:
print("Good morning!")
print("12-hour clock: Time is: " + str(h) + ":" + str(m) + "a.m.")
else:
h = h - 12
print("Good evening!")
print("12-hour clock: Time is: " + str(h) + ":" + str(m) + "p.m.")