+ 2
Whay this code not working
Import time If time == 12:00 Print ("Happy New Year")
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!
+ 10
Colon :
Semicolon ;
😉
+ 7
Er, SHIVAM , I said that a day ago 😆
+ 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
+ 3
I got confused right there 😅. Thanks for the correction!
+ 3
Put semicolons and maybe it will work 🤗
+ 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!")
+ 2
That's not the solution of the Aryan's problem BLUE....
+ 2
Colon :
And semicolon ;
+ 1
am learning JavaScript i forget the basics of python so 😅
- 3
You can simply code
time = 12:00
print(time)