Solved

Python if statement II

Posted on 2015-01-15
6
135 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 48

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
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 
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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Suggested Solutions

Variable is a place holder or reserved memory locations to store any value. Which means whenever we create a variable, indirectly we are reserving some space in the memory. The interpreter assigns or allocates some space in the memory based on the d…
This article will show the steps for installing Python on Ubuntu Operating System. I have created a virtual machine with Ubuntu Operating system 8.10 and this installing process also works with upgraded version of Ubuntu OS. For installing Py…
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 explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

707 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

12 Experts available now in Live!

Get 1:1 Help Now