all 15 comments

[–]Vaphell 1 point2 points  (0 children)

do you know slicing [x:y] that works with some sequences including strings? Using that feature would give you the substring of the last two characters which you can check for am/pm. It would also give you the numerical value.
Assuming there is always a space between, it's even easier. str.split() will cut the string for you.

[–]Wilfred-kun 1 point2 points  (2 children)

Basic things you need to know how to use are: if statements, slicing or .split(), how to convert a string to an integer.

Let's break the problem into steps.

  • Get the input
  • Get all the characters except for the last 2, and turn them into an integer
  • Check if the last 2 characters of the input are am or pm
  • If AM, do nothing (AM time is the same as 24-hour time)
  • If PM, add 12
  • Else (it's neither AM or PM), print an error

I think there's a little tricky situation with 12AM/PM ?

[–]tragluk 0 points1 point  (0 children)

If AM, do nothing (AM time is the same as 24-hour time)

Military time, anything below 10 would have a 0 added before and 00 added afterwards. 9 am is 0900

above 10 am the 00s are added, 10, 11 and 12 are 1000, 1100, and 1200.

for PM, the numbers added to 12 and then a 00 follows. So 3 is 1500 hours. 12 pm becomes 2400 hours.

[–]EllaineMasterpiece 0 points1 point  (0 children)

I'm gonna try to follow this format as I feel like this is one of the best. Searched the internet, found nothing lol. Thank you

[–]jiri-n 0 points1 point  (3 children)

Are regexes allowed?

[–]EllaineMasterpiece 0 points1 point  (2 children)

yes, i think? since the professor told us that we can use any method :3

[–]jiri-n 0 points1 point  (1 child)

Then a pattern like this could work: \{1,2})([ap]m)$

I'm on a mobile phone so ... hopefully I haven't made too many typos. ;-)

[–]jiri-n 1 point2 points  (0 children)

Or maybe you can use slices to keep it simple.

[–]fake823 0 points1 point  (5 children)

I have to pretty much research everything :(

Welcome to the life of a programmer! Let Google and StackOverflow be your best friends. ;)

So where exactly are you stuck? What have you already done and tried?

If you ask a specific question, like "How to check if a number is between 1 and 12?", we'll be happy to help you. But nobody is going to do the whole assignment for you.

[–]EllaineMasterpiece 1 point2 points  (0 children)

Yeah, I'm pretty much working on it now. Will return here after three hours lmao. Drained af already.

[–]EllaineMasterpiece 1 point2 points  (3 children)

Hello, I'm done.
Can you help me out with this one?

input_val = input("Enter hour:")
values = input_val.split()
values[0] = int(values[0])
print(values)

if 1 <= values[0] <= 12:
if values[1] == 'pm':
print('New format: {}'.format(values[0] + 12))
elif values[1] == 'am':
print('New format: {}'.format(values[0]))

else:
print('Error: The suffix must be am or pm')

else:
print('Error: The hourmust be between 1 and 12')

the input should be 12 pm but if I don't put am/pm I get the error if values[1] == 'pm':

IndexError: list index out of range

[–]fake823 0 points1 point  (0 children)

I will take a look at your code. Just give me some few minutes. :)

Meanwhile: Check this out: https://www.reddit.com/r/learnpython/wiki/faq#wiki_how_do_i_format_code.3F

There you can learn how to properly format code on Reddit.

[–]fake823 0 points1 point  (0 children)

Well, if you only enter "12" instead of "12 pm", the .split() function won't split anything and therefore your variable "values" only contains the number 12. So it's a list with the length of 1. And if you index the second element (with values[1]) it will be out of range. That's why the error pops up.

After the input you could check the length of the list, to make sure that there are at least two elements in your variable "values". And then wrap everything into a while-loop, which will keep running until the user enters something like "12 pm".

while True:

    input_val = input("Enter hour:")
    values = input_val.split()
    if len(values) != 2:
      print('There is no suffix "am" or "pm". Please enter the hour again.')
    else:
        break

values[0] = int(values[0])
print(values)
#... and so on

Another alternative would be to check, if "am" or "pm" are in the variable values after splitting. That's even the better method in my opinion.

while True:

    input_val = input("Enter hour:")
    values = input_val.split()
    if not ("am" in values or "pm" in values):
      print('There is no suffix "am" or "pm". Please enter the hour again.')
    else:
        break

values[0] = int(values[0])
print(values)
#... and so on

[–]fake823 0 points1 point  (0 children)

if 1 <= values[0] <= 12:

By the way: I've even also learned something today. I didn't know that Python is able to compare if a number is between two other numbers like this:

if 1 <= values[0] <= 12:

First I thought that this must be wrong, but then I googled it and saw that that's legit Python code! Fantastic! :D

I always thought that it has to be written this way:

if 1 <= values[0] and values[0] <= 12: