Solved

Python if statement II

Posted on 2015-01-15
6
138 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
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
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

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

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

"The time has come," the Walrus said, "To talk of many things: Of sets--and lists--and dictionaries-- Of variable kinks-- And why you see it changing not-- And why so strange are strings." This part describes how variables and references (see …
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…
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.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

831 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