Python 3.4.1 how to get 1 of 01

So, assigning a class to an instructor requires us to create a program that looks like a time clock. You enter the start time and enter the time you leave, and the program determines how much the person gets paid. However, the program must be in a 24 hour format, for example if you want to dial 2 pm you need to enter 14:00

. With this logic, what if you have to work until the early morning hours, for example 01:00

? I have a program asking the user for the time:

start = input("Enter the start time:")
end = input("Enter the end time:)

      

I then use start.split(":")

and end.split(":")

to create a split list ":"

and then use eval()

to get an integer, but whenever I try to enter an integer with a 0 in front of it (for example 01

) the program responds with a syntax error and that it is an invalid token.

Is there a way to get around this to get the value of the number?

+3


source to share


2 answers


Do not use eval()

. Use a function to parse strings that represent integers. int()

You can safely pass zero numbers to int()

; int('01')

returns 1:



>>> int('01')
1
>>> int('12')
12

      

+3


source


There is another way you can split the values. You can use the functionstr.startswith

start = input("Enter the start time:")
end = input("Enter the end time:")

if (str.startswith(start,"0")):
    print(start[1:])

      



OUTPUT:

Enter the start time:01:00
Enter the end time:02:00
1:00

      

+1


source







All Articles