+ 2

Whay this code not working

Import time If time == 12:00 Print ("Happy New Year")

1st Jan 2021, 3:30 AM
Aryan Raj Ayush
Aryan Raj Ayush - avatar
11 ответов
+ 9
Bugs/Errors: 1. When importing a module you cannot use the module name, instead use its functions and attributes. 2. colons between integers causes an invalid syntax. 3. It is better to use datetime module if you are comparing a specific time (e.g. minute or hour) 4. colon missing at the end of if condition - - - - - - - - - - - - - - - - Try this: import datetime date = datetime.datetime.now() if date.hour == 12: print("Happy New Year!") - - - - - - - - - - - - - - - - - # Note that in datetime module, date.hour attribute uses 24 hour-format which means 1 <= hour <= 24. I hope this helps. If you have more questions, feel free to ask. Happy Coding!
1st Jan 2021, 3:49 AM
noteve
noteve - avatar
+ 10
Colon : Semicolon ; 😉
1st Jan 2021, 3:54 AM
David Ashton
David Ashton - avatar
+ 7
Er, SHIVAM , I said that a day ago 😆
2nd Jan 2021, 4:20 PM
David Ashton
David Ashton - avatar
+ 4
Aryan Raj time == 12:00 is not a valid syntax. Please refer to the python course on using if statement. Further, you can refer this link for the time module. https://www.tutorialspoint.com/python/python_date_time.htm
1st Jan 2021, 3:41 AM
Art1mis
+ 3
I got confused right there 😅. Thanks for the correction!
1st Jan 2021, 3:56 AM
noteve
noteve - avatar
+ 3
Put semicolons and maybe it will work 🤗
2nd Jan 2021, 5:28 PM
Riselda Rada
Riselda Rada - avatar
+ 2
In addition to what《 Nicko12 》 says, you can add an else statement if the time isn't 12 yet like this: else: print("Not time yet") Elif statements were optional too. Example: elif date.hour >= 10: print("About 2 hours left!") elif date.hour >= 11: print("About 1 hour left!")
1st Jan 2021, 4:13 AM
Dino Wun (Use the search bar plz!)
Dino Wun (Use the search bar plz!) - avatar
+ 2
That's not the solution of the Aryan's problem BLUE....
2nd Jan 2021, 5:16 AM
Dino Wun (Use the search bar plz!)
Dino Wun (Use the search bar plz!) - avatar
+ 2
Colon : And semicolon ;
2nd Jan 2021, 2:58 PM
SHIVAM
SHIVAM - avatar
+ 1
am learning JavaScript i forget the basics of python so 😅
2nd Jan 2021, 5:21 AM
BLUE
BLUE - avatar
- 3
You can simply code time = 12:00 print(time)
2nd Jan 2021, 5:14 AM
BLUE
BLUE - avatar