2

I'm a french newbie in Python and I would like to code a program which warns us when the time (string "Day, Hours, Minutes, Seconds") is wrong (for example, 83 seconds). I did this program:

t=input("Put day,hours,minutes,seconds: ")
t="j,h,m,s"
if int(t[6])<=0 or int(t[6])>=59:
    print("Seconds wrong")
if int(t[4])<=0 or int(t[4])>=59:
    print("Minutes wrong")
if int(t[2])<=0 or int(t[2])>=24:
    print("Hours wrong")
if int(t[0])<=0 or int(t[0])>=31:
    print("days wrong")
else: 
    print("OK")

But I've this error:

  if t[6]<=0 or t[6]>=59:
TypeError: unorderable types: str() <= int()

So I put "int" everywhere (like "int(t[X])<=0") but then I've this error:

  if int(t[6])<=0 or int(t[6])>=59:
ValueError: invalid literal for int() with base 10: 's'
3
  • 2
    You first ask for t from user input, then rebind t to a new string, with no integers in it. What do you expect to happen instead? What user input would be given? Commented Oct 27, 2013 at 13:21
  • remove the second line, it's rewriting t as "j,h,m,s" Commented Oct 27, 2013 at 13:22
  • Last I checked, 0 and 59 are valid seconds and minutes. You likely want something more like t[6] < 0 or t[6] >= 60. Same with the other time units. Commented Oct 27, 2013 at 13:27

2 Answers 2

2

There are no digits in this string:

t="j,h,m,s"

so any attempt to do int(t[anything]) is going to fail. You can't convert a string to an integer unless the string contains a string representation of an actual integer, such as t = "1234".

Moreover, even if you had something like t = "31,11,22,45", then int(t[6]) would not give you the number of seconds, because the representation of seconds would be at indices 9 and 10. You'd need t = int(t[9:11]) in that case.

Something like this is what you're looking for:

#!/usr/bin/python

t = "31,11,22,45"
(day, hour, min, sec) = [int(elem) for elem in t.split(',')]

if not 0 <= sec <= 59:
    print("Seconds wrong")
elif not 0 <= min <= 59:
    print("Minutes wrong")
elif not 0 <= hour <= 23:
    print("Hours wrong")
elif not 1 <= day <= 31:
    print("days wrong")
else:
    print("OK")

Note you either need to change all but your first if to elif, otherwise it'll print "OK" if day is correct but everything else is wrong, or you'll need to keep some kind of separate variable to store if anything is wrong, and check that at the end, such as in the following:

#!/usr/bin/python

t = "31,11,22,45"
(day, hour, min, sec) = [int(elem) for elem in t.split(',')]
time_ok = True

if not 0 <= sec <= 59:
    print("Seconds wrong")
    time_ok = False

if not 0 <= min <= 59:
    print("Minutes wrong")
    time_ok = False

if not 0 <= hour <= 23:
    print("Hours wrong")
    time_ok = False

if not 1 <= day <= 31:
    print("Days wrong")
    time_ok = False

if time_ok:
    print("Time is OK")
Sign up to request clarification or add additional context in comments.

13 Comments

@kren470: Yes, he could.
Yes I forget the elif, thanks. But how to ask days, hours... in the "DD,HH,MM,SS" format ?
@Valentln: I don't really understand the question, you could just ask for it: t = input('Enter the time in DD,HH,MM,SS format:')
That's that ! I would like to ask day, hours.. in 4 steps: "Say Days" and the user taps the days. Then "Say hours".... Thanks you very much for your help !
You could do days = int(input("Say days: ")) and again for hours, minutes and seconds.
|
2

Nice effort, but be careful with the second line:

t = "j,h,m,s"

This overwrites the user input, and assigning "j,h,m,s" to t instead. Remove it!

Also, you can't compare it using or. Just check if int(t[6]) > 59 is sufficient.

if int(t[6]) > 59:
    print("seconds wrong") 

A better way to get the comma-separated numbers is to use string.split() method.

t = t.split(',') #split the string in the commas

Now, you don't have to worry about the comma counts. The day would be t[0], the hours would be t[1], and so on.

Ah, also one more thing. You don't have to use if statements repeatedly there. Use it on the first time, and change the next ones with elif statements.

Full fixed code:

t = input("Put day,hours,minutes,seconds: ")
if int(t[6])>59:
    print("Seconds wrong")
elif int(t[4]) < 0 or int(t[4]) > 59: 
    print("Minutes wrong")
elif int(t[2]) < 0 or int(t[2]) > 24:
    print("Hours wrong")
elif int(t[0]) < 0 or int(t[0]) > 31:
    print("days wrong")
else: 
    print("OK")

To be honest, as Paul mentioned, this still won't work if any input is more than one digit.

You'll have to use string.split() to achieve this.

t = input("Put day,hours,minutes,seconds: ").split(",")

if int(t[3])>59:
    print("Seconds wrong")
elif int(t[2]) < 0 or int(t[2]) > 59: 
    print("Minutes wrong")
elif int(t[1]) < 0 or int(t[1]) > 24:
    print("Hours wrong")
elif int(t[0]) < 0 or int(t[0]) > 31:
    print("days wrong")
else: 
    print("OK")

3 Comments

I'm not yet used to the string format, but I understand what you mean. And I know how to split with comas, thanks for your help !
Your "fixed code" won't work if the user enters more than one digit for any seconds, minutes, hours, or days.
Right. I fixed the syntax, but the logic is wrong. Well, i've suggested the working way now.

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.