Reputation: 35
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'
Upvotes: 2
Views: 112
Reputation: 25936
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")
Upvotes: 2
Reputation: 28312
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")
Upvotes: 2