TransWikia.com

String concatenation attempt causing unexpected error

Stack Overflow Asked by Q Q on December 4, 2021

I’m new to python and overflow so bear with me please.

import time
import datetime
    
now =  datetime.datetime.now()
   
if str(now.hour<12):
    print(str(now.hour -  12 + ":"))
error: unsupported operand type(s) for +: 'NoneType' and 'str'

4 Answers

I think the bigger problem is that you are casting to string in your if condition. Since a non empty string will be truthy, your condition will always result in true. The code should be:

import time
import datetime
    
now =  datetime.datetime.now()
   
if now.hour < 12:
    print(str(now.hour -  12) + ":"))

Answered by razdi on December 4, 2021

You have to modify the code as follows:

import time
import datetime

now =  datetime.datetime.now()

if str(now.hour<12):
    print(str(now.hour -  12) + ":") #moved parenthesis inside

Answered by Joe Ferndz on December 4, 2021

Try

print(str(now.hour -  12) + ":")

Answered by joe on December 4, 2021

You've got your parenthesis in the wrong place. You want

str(now.hour-12)+":"

Answered by Daniel Walker on December 4, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP