?
Solved

Python if statement II

Posted on 2015-01-15
6
Medium Priority
?
152 Views
Last Modified: 2015-01-15
Hello Expert,

Working on if statement in Python

import time
import datetime
ts = time.time()
month = datetime.datetime.fromtimestamp(ts).strftime('%m')
print month
if month == 01:
    print "January"

Expected
01
January

Result
01

Actually this is two questions
1. Why does it not print out 'January'?

2. When an error in syntax is made the IDLE
returns a message like
Traceback (most recent call last):
  File "F:/Archive/Python/date_3.py", line 2, in <module>
    month = datetime.datetime.fromtimestamp(ts).strftime('%m')
NameError: name 'datetime' is not defined
But other times it doesn't return anything, it just
doesn't work. Why is that?

Thanks

Allen in Dallas
0
Comment
Question by:Allen Pitts
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 
LVL 52

Accepted Solution

by:
Rgonzo1971 earned 1000 total points
ID: 40551357
Hi,

Isnt it
if month == '01':

Regards
0
 
LVL 29

Assisted Solution

by:pepr
pepr earned 1000 total points
ID: 40551366
Rgonzo is right. If in doubt, you can always try print repr(month). It will print the representation of the value. If it is a string, the value will be printed with the quotes.

The repr() is a built-in function that returns representation of a value. The representation (if possible) is the string value that--when copy/pasted into a source code--would produce the object with the same value.
0
 

Author Closing Comment

by:Allen Pitts
ID: 40551405
Thanks guys. The parenthesis did it.
0
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 29

Expert Comment

by:pepr
ID: 40551437
@9apit: Just curious, where was the problem with parentheses? I cannot see it.
0
 
LVL 17

Expert Comment

by:gelonida
ID: 40551620
I guess it's just a language mixup between the two words "parenthesis" and "quotes"

I can only support pepr's suggestion to use repr during debugging.


so

print("A is %r" % a)
or
print ("A is", repr(a))

gives you often the essential missing bitcompared to:
print("A is %s" % a)
or print("A is", a)

Replr allows you to see the difference between a string and an integer
or between the two strings

"hello" and "hello "

please note also, that for example 010 is NOT the same as 10, though 01 is the same as 1
a leading zero indicates an octal number, which is almost never what you want to do
0
 

Author Comment

by:Allen Pitts
ID: 40552231
Hello pepr,
This the code I got to work

import time
import datetime
ts = time.time()
month = datetime.datetime.fromtimestamp(ts).strftime('%m')
print month
if month == '01':
    print "January"

Before the second to last line was
if month == 01:

I added single quotes
if month == '01':

to get the January to print.

Allen
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Strings in Python are the set of characters that, once defined, cannot be changed by any other method like replace. Even if we use the replace method it still does not modify the original string that we use, but just copies the string and then modif…
Article by: Swadhin
Introduction of Lists in Python: There are six built-in types of sequences. Lists and tuples are the most common one. In this article we will see how to use Lists in python and how we can utilize it while doing our own program. In general we can al…
Learn the basics of modules and packages in Python. Every Python file is a module, ending in the suffix: .py: Modules are a collection of functions and variables.: Packages are a collection of modules.: Module functions and variables are accessed us…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

719 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question