Python Forum
Syntax error in if statement
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Syntax error in if statement
#1
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)?
Reply
#2
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)
Reply
#3
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))
Output:
timeh = 10010 x = 00010 x = 11000
Lewis
To paraphrase: 'Throw out your dead' code. https://www.youtube.com/watch?v=grbSQ6O6kbs Forward to 1:00
Reply
#4
Thanks a lot! It really helped me out! :)
Reply
#5
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))
Output:
mynow = 2018-06-26 14:46:16.202998 Decimal hour = 14 Binary hour = 01110
Lewis
To paraphrase: 'Throw out your dead' code. https://www.youtube.com/watch?v=grbSQ6O6kbs Forward to 1:00
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Syntax error for "root = Tk()" dlwaddel 15 1,007 Jan-29-2024, 12:07 AM
Last Post: dlwaddel
Photo SYNTAX ERROR Yannko 3 333 Jan-19-2024, 01:20 PM
Last Post: rob101
  An unexplainable error in .format statement - but only in a larger piece of code? ToniE 4 656 Sep-05-2023, 12:50 PM
Last Post: ToniE
  Syntax error while executing the Python code in Linux DivAsh 8 1,450 Jul-19-2023, 06:27 PM
Last Post: Lahearle
  Code is returning the incorrect values. syntax error 007sonic 6 1,135 Jun-19-2023, 03:35 AM
Last Post: 007sonic
  syntax error question - string mgallotti 5 1,248 Feb-03-2023, 05:10 PM
Last Post: mgallotti
  Syntax error? I don't see it KenHorse 4 1,194 Jan-15-2023, 07:49 PM
Last Post: Gribouillis
  Syntax error tibbj001 2 847 Dec-05-2022, 06:38 PM
Last Post: deanhystad
  Python-for-Android:p4a: syntax error in main.py while compiling apk jttolleson 2 1,776 Sep-17-2022, 04:09 AM
Last Post: jttolleson
  Error in Using INPUT statement gunwaba 1 2,015 Jul-03-2022, 10:22 PM
Last Post: deanhystad

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020