Solved

Python if statement II

Posted on 2015-01-15
6
137 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:9apit
6 Comments
 
LVL 49

Accepted Solution

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

Isnt it
if month == '01':

Regards
0
 
LVL 28

Assisted Solution

by:pepr
pepr earned 250 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:9apit
ID: 40551405
Thanks guys. The parenthesis did it.
0
Zoho SalesIQ

Hassle-free live chat software re-imagined for business growth. 2 users, always free.

 
LVL 28

Expert Comment

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

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:9apit
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

Zoho SalesIQ

Hassle-free live chat software re-imagined for business growth. 2 users, always free.

Question has a verified solution.

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

Flask is a microframework for Python based on Werkzeug and Jinja 2. This requires you to have a good understanding of Python 2.7. Lets install Flask! To install Flask you can use a python repository for libraries tool called pip. Download this f…
I'd like to talk about something that is near and dear to my heart: build systems. Without them, building software is all about compiling locally, with software versions everywhere. It can be a mess. Today we are going to discuss building a small di…
Learn the basics of if, else, and elif statements in Python 2.7. Use "if" statements to test a specified condition.: The structure of an if statement is as follows: (CODE) Use "else" statements to allow the execution of an alternative, if the …
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

911 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now