0

Define a function, time(x,y), that takes time in hours and minutes rounds it to the nearest 5 minutes and expresses it as a word/string.

For example:

time(7,5) returns "five after seven"
time(7,13) returns "quarter after seven"
time(7,9) returns "ten after seven"
time(7,20) returns "twenty after seven"
time(7,24) returns "twenty five after seven"
time(7,30) returns "half past seven"
time(7,36) returns "twenty five to eight"
time(7,38) returns "twenty to eight"
time(7,45) returns "quarter to eight"
time(7,50) returns "ten to eight"
time(7,56) returns "five to eight"
time(12,00) returns "noon"
time(18,20) returns "twenty after six"
time(23,45) returns "a quarter to midnight"

My plan to code this was to use an array for the hours and if statements and append the minutes. I started with my code but I'm having troubles making it work

def time(h):
    words=["midnight","one","two","three","four","five","six","seven","eight","nine","ten","eleven","noon"]
    for n in range(len(words)):
        h = (words[n])
    return h

When I simply run just this code, it give me 'noon' every time. I know I am doing something wrong, can someone please help my fix this and give me a start in the right direction for the minutes.

Thanks

1
  • 2
    The logic of for n in range(len(words)): seems wrong. Try return words[h] directly and see what happens. Commented Sep 19, 2014 at 0:40

3 Answers 3

2

The logic in your loop for n in range(len(words)): seems wrong. You're just looping each element and assign it to h (and at last h remains the last value which is "noon").
Something like this would be a good start:

def time(h):
    words = [
        "midnight", "one", "two", "three", "four", "five", "six", "seven",
        "eight", "nine", "ten", "eleven", "noon"]
    return words[h]

Or keep using the loop of yours (not recommended):

def time(h):
    words = [
        "midnight", "one", "two", "three", "four", "five", "six", "seven",
        "eight", "nine", "ten", "eleven", "noon"]
    ret = None
    for i in range(len(words)):
        if i == h:
            ret = words[i]
    return ret
Sign up to request clarification or add additional context in comments.

3 Comments

Thank so much and do you have any idea for the minutes? Is using if statements a good idea?
@Drake For minutes please see Cyber's answer. :) However I believe it would be much more fun if you try it yourself first (and some failures make it even more satisfiable cuz you'll find that you really solve problems :D)
I'm Cyber and I support the above message :) (the part about trying an implementation yourself first)
1

I'm sure I might have missed a few edge cases, but you could use this general idea.

hours = ["midnight","one","two","three","four","five","six","seven","eight","nine","ten","eleven","noon","one","two","three","four","five","six","seven","eight","nine","ten","eleven"]
minutes = ["", "five", "ten", "quarter", "twenty", "twenty five", "half", "twenty five", "twenty", "quarter", "ten", "five"]

def time(h,m):
    if m < 2 or m > 58:
        return str(hours[h])
    elif m <= 30:
        return str(minutes[int(round(float(m)/5))]) +  " past " + str(hours[h])
    else:
        return str([int(round(float(m)/5))]) + " to " + str(hours[h+1])

Testing

>>> time(7,5)
'five past seven'

>>> time(7,13)
'quarter past seven'

>>> time(12,15)
'quarter past noon'

>>> time(21,45)
'quarter to ten'

4 Comments

For testing time(7, 5) it gives me ten past seven and it also does it for the rest of the testing I tried, the rounding is off
Oops! I forgot to add an empty string in the minutes list, see my most recent edit. Also you can adjust the first if to handle rounding within +- 2 minutes of the hour.
Thanks mate, and if I wanted to print these in strings i.e 'five past seven' as oppose to what we already have. Do I just say print str(minutes......etc) or does that not work?
@Drake yes, you can convert the ints to str, then concatenate them together in the if statements, then return the final string. Therefore you can call the function and get a string back. I updated my post with this behavior.
0

You're over writing h every time you run this your for loop. It will return noon because noon is the last n in your loop.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.