ReadTime


#1

Hello eveybody I am trying to do this exercise, but I do not understand why every time my code gives an error it does not asks for a new input:
Create function read hour ( NO input - parameters) As TRACK user Entering a Time With time , minutes and seconds quoted separately as quoted in example after . The function will ensure that the user Enters a proper time . The function should give an error for unchanged Incorrect user input and prompting the user To state time , minutes, or seconds again IF 's Not Correct . Function Should return a list with three items [ hour , minute, second ] .

and here is my code:

def readTime():
hour = -1
while hour<0 or hour>23:
hour = int(input("Enter hour: "))
if hour<0 or hour>23:
print("- ERROR: Hour must be between 0 and 23!")
minute = -1
while minute<0 or minute>59:
minute = int(input("Enter minute: "))
if minute<0 or minute>59:
print("- ERROR: Minute must be between 0 and 59!")
sec = -1
while sec<0 or sec>59:
sec = int(input("Enter second: "))
if sec<0 or sec>59:
print("- ERROR: Second must be between 0 and 59!")
return [hour,minute,sec]



Replace this line with your code.


#2

When I run your code:

def readTime():
    hour = -1
    while hour<0 or hour>23:
        hour = int(input("Enter hour: "))
        if hour<0 or hour>23:
            print("- ERROR: Hour must be between 0 and 23!")
    minute = -1
    while minute<0 or minute>59:
        minute = int(input("Enter minute: "))
        if minute<0 or minute>59:
            print("- ERROR: Minute must be between 0 and 59!")
    sec = -1
    while sec<0 or sec>59:
        sec = int(input("Enter second: "))
        if sec<0 or sec>59:
            print("- ERROR: Second must be between 0 and 59!")
    return [hour,minute,sec]
    
print readTime()

It works just fine. Could you post a link to the exercise?


#3

I found the problem, the exercise in norwegian so I do not think it will help u a lot.. But a lot for your help :slight_smile:


#4

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.