Edward LaPiere
Edward LaPiere

Reputation: 27

If and elif are not printing

print'Personal information, journal and more to come'
x = raw_input()
if x ==("Personal Information"): # wont print 
 print' Edward , Height: 5,10 , EYES: brown , STATE: IL TOWN:  , SS:'
elif x ==("Journal"):  # wont print 
 read = open('C:\\python\\foo.txt' , 'r')
 name = read.readline()
 print (name)

I start the program and "Personal information, journal and more to come" shows but when I type either Personal information or journal neither of them print the result and I'm not getting any errors.

Upvotes: 0

Views: 199

Answers (3)

mergesort
mergesort

Reputation: 672

You are typing in Personal information, when the if statement is expecting Personal Information (with a capital I for information).

What you can do (what Ignacio above is eluding to) is do:

if x.lower() == ("Personal Information").lower():

instead of:

if x == ("Personal Information"):

then any case, "Personal information", "personal information", "personAL infoRmation", and so on, will match and go into the if statement. The reason this works is because when this executes, it will take the value of x, and make it a lowercase string, and the string "Personal information" and make it a lowercase string, so now no matter what the case originally was, they both will be lowercase when comparing.

foo and bar are examples, common programming nomenclature in programming. It is just an example of any variable, x, y, z, etc, could have been used just as easily, but foo and bar are just common variables to refer to.

Upvotes: 0

Philluminati
Philluminati

Reputation: 2789

Works for me. Are you writing "Personal Information" with a capital I?

print'Personal information, journal and more to come'
x = raw_input()
if x == ("Personal Information"): # wont print
    print' Edward , Height: 5,10 , EYES: brown , STATE: IL TOWN:  , SS:'
elif x ==("Journal"):  # wont print
    read = open('C:\\python\\foo.txt' , 'r')
    name = read.readline()
    print (name)

output:

[00:20: ~$] python py
Personal information, journal and more to come
Journal
Traceback (most recent call last):
  File "py", line 8, in <module>
    read = open('C:\\python\\foo.txt' , 'r')
IOError: [Errno 2] No such file or directory: 'C:\\python\\foo.txt'
[00:20: ~$] python py
Personal information, journal and more to come
Personal Information
 Edward , Height: 5,10 , EYES: brown , STATE: IL TOWN:  , SS:
[00:20: ~$] 

Perhaps it's the formatting? I'm using 4 white spaces.

Upvotes: 0

Ignacio Vazquez-Abrams
Ignacio Vazquez-Abrams

Reputation: 798566

when i type either Personal information or journal

Well, yeah. It isn't expecting either of those; your case is wrong.

To perform a case-insensitive comparison, convert both to the same case first.

if foo.lower() == bar.lower():

Upvotes: 4

Related Questions