top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

if and elif statements not working in Python?

+1 vote
336 views
def town():
 print ("You stand in the middle of Coffeington while you descide what"
 " to do next, you have herd rumor of the Coffeington Caves that run"
 "under the city, would you like to check them out?")
 answer = input()
 if answer == ("yes") or ("Yes") or ("y"):
 print("You set out for the Coffeington Caves")
 elif answer == ("no") or ("No") or ("n"):
 print("Oh...well im sure you can find something else to do")
 else:
 print("You just stand there")
town()

I don't know why the "elif" or "else" part of the "if statment" wont trigger. what ends up happening is that regardless of what answer you put in input it will always print out "you set out for the Coffeington Caves". whats supposed to happen is if you say "no" it should just end?

posted Aug 8, 2013 by Sonu Jindal

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

1 Answer

+1 vote
if answer == ("yes") or ("Yes") or ("y"):

This doesn't do what you think it does. First it compares answer to "yes". Then it takes the result of that and OR's it with "Yes". Then it takes the result of that and OR's it with "y". Finally it takes the bool of the result and decides whether to execute the if-body. Since those OR's will always be true, it always executes the if-body, and never the elif or else body.

Fix the expression to what you presumably meant:

if answer == "yes" or answer == "Yes" or answer == "y":
Or less typing:    
if answer in ("yes", "Yes", "y"):
answer Aug 8, 2013 by Meenal Mishra
Similar Questions
+2 votes

I want to use the Python 3.4 interpreter interactively (with centos 5), via a PuTTY ssh session. Currently, the arrow keys do not work:

$ /usr/local/bin/python3.4 

Python 3.4.2 (default, Feb 11 2015, 15:06:33) 

[GCC 4.1.2 20080704 (Red Hat 4.1.2-55)] on linux 

Type "help", "copyright", "credits" or "license" for more information. 

>>> ^[[A 

I tried the following thread also
http://stackoverflow.com/questions/893053/python-shell-arrow-keys-do-not-work-on-remote-machine

which suggests that the problem can be fixed by installing the readline package followed by the rebuild of Python. Can anyone comment on the easiest way to fix this? Is a rebuild of Python necessary?

+4 votes

I have an application which sets up logging after parsing the args in the main() funktion. It needs to be setup after parsing the args because I can set the loglevel via commandline flags.

I have tried many variants on how to do that but every time with an weird result. What I want is logging in from all libs and really understand that doing this should be enough there:

from logging import getLogger

logger = getLogger(__name__)

But, I need to setup the logger in the main() function to log only to a file and not to console because my application has an own shell interface which should not be spammed with log messages - never a message should show up there.

I think it should be only some few lines of code but I can't figure that out. The logger should be configured to have a max file size and rotate logfiles. Can someone help me with this?

+1 vote

Let's say I want to compare two csv files: file A and file B. They are both similarly built - the first column has product IDs (one product per row) and the columns provide some stats about the products such as sales in # and $.

I want to compare these files - see which product IDs appear in the first column of file A and not in B, and which in B and not A.
Finally, it would be very great if the result could be written into two new CSV files - one product ID per row in the first column. (no other data in the other columns needed)

This is the script I tried:

import csv

#open CSV's and read first column with product IDs into variables pointing to lists
A = [line.split(',')[0] for line in open('Afile.csv')]
B = [line.split(',')[0] for line in open('Bfile.csv')]

#create variables pointing to lists with unique product IDs in A and B respectively 
inAnotB = list(set(A)-set(B))
inBnotA = list(set(B)-set(A))

print inAnotB
print inBnotA

c = csv.writer(open("inAnotB.csv", "wb"))
c.writerow([inAnotB])

d = csv.writer(open("inBnotA.csv", "wb"))
d.writerow([inBnotA])

print "done!" 

But it doesn't produce the required results.
It prints IDs in this format:

247158132n

and nothing to the csv files.

...