![]() |
Syntax error in if statement - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: Syntax error in if statement (/thread-11151.html) |
Syntax error in if statement - Kve - Jun-25-2018 So I'm a quite new python programmer and I'm now working on a digital binary clock (it should convert the hour and minute in binary numbers). I was trying to add some 0's in front of the binary outputs with a length less than 5 for the hours (because 24, i.e. the greatest number, is 11000 in binary; I wanted to have a constant length of the outputs). The problem is that Python recognises the first thing (symbol, variable, ect.) after "for i in range(5-len(timem)" as a syntax error (whatever I do: if I delete the ":" or everything after "for i in range(5-len(timem)") in: while True: timeh = "{0:b}".format((int(time.ctime()[11:13])) for i in range(5-len(timeh)): timeh = "0" + timeh timem = "{0:b}".format((int(time.ctime()[14:16])) for i in range(5-len(timem)): timem = "0" + timem print(timeh + " " + timem)Could anyone help me please (in a way or in another)? RE: Syntax error in if statement - gontajones - Jun-25-2018 Here I've got 2 syntax errors, missing ')'s at line 2 and 6. import time while True: timeh = "{0:b}".format((int(time.ctime()[11:13]))) for i in range(5 - len(timeh)): timeh = "0" + timeh timem = "{0:b}".format((int(time.ctime()[14:16]))) for i in range(5 - len(timem)): timem = "0" + timem print(timeh + " " + timem) RE: Syntax error in if statement - ljmetzger - Jun-25-2018 Try syntax like the following in Python 3: import time timeh = int(time.ctime()[11:13]) print("timeh = {:05b}".format(timeh)) x = 2 print("x = {:05b}".format(x)) x = 24 print("x = {:05b}".format(x)) Lewis
RE: Syntax error in if statement - Kve - Jun-25-2018 Thanks a lot! It really helped me out! :) RE: Syntax error in if statement - ljmetzger - Jun-26-2018 Another simpler method uses the datetime library which comes with python. Reference: https://docs.python.org/3/library/datetime.html import datetime mynow = datetime.datetime.now() print("mynow = {}".format(mynow)) print("Decimal hour = {} Binary hour = {:05b}".format(mynow.hour, mynow.hour)) Lewis
|