1

I want to input a time in the form HHMM to one of my programs in Python but when I do, I get

"SyntaxError: invalid token",

I think this is because it is not a valid octal number, as I have seen on other websites, for example.

The code I'm using is:

time1 = float(input("Please enter time 1:"))
Bhavesh Odedra
  • 10,990
  • 12
  • 33
  • 58
user4690602
  • 129
  • 1
  • 2
  • 4

1 Answers1

2

You should look into the datetime module to convert your string to an actual datetime object, which has lots of useful methods:

>>> import datetime
>>> time = datetime.datetime.strptime(raw_input('specify time in HHMM format: '), "%H%M")
specify time in HHMM format: 0830
>>> time
datetime.datetime(1900, 1, 1, 8, 30)
>>> time.time()
datetime.time(8, 30)
>>> time.hour
8

Using Python2.7 here (hence: raw_input).

The SyntaxError you're observing is also explained by that link: you're using input, rather than raw_input in Python2.x.

Oliver W.
  • 13,169
  • 3
  • 37
  • 50