Solved

Python if statement II

Posted on 2015-01-15
6
142 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 50

Accepted Solution

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

Isnt it
if month == '01':

Regards
0
 
LVL 29

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
Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

 
LVL 29

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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering 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

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…
When we want to run, execute or repeat a statement multiple times, a loop is necessary. This article covers the two types of loops in Python: the while loop and the for loop.
Learn the basics of lists in Python. Lists, as their name suggests, are a means for ordering and storing values. : Lists are declared using brackets; for example: t = [1, 2, 3]: Lists may contain a mix of data types; for example: t = ['string', 1, T…
Learn the basics of while and for loops in Python.  while loops are used for testing while, or until, a condition is met: The structure of a while loop is as follows:     while <condition>:         do something         repeate: The break statement m…

792 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