Deivore
Deivore

Reputation: 29

Using if/elif/else for a "switch" in python

While using the following code:

url = None
print("For 'The Survey of Cornwall,' press 1")
print("For 'The Adventures of Sherlock Holmes,' press 2")
print("For 'Pride and Prejudice,' press 3")
n = input("Which do you choose?")
if n==1:
    url = 'http://www.gutenberg.org/cache/epub/9878/pg9878.txt' #cornwall
    print("cornwall")
elif n==2:
    url = 'http://www.gutenberg.org/cache/epub/1661/pg1661.txt' #holmes
    print("holmes)
elif n==3:
    url = 'http://www.gutenberg.org/cache/epub/1342/pg1342.txt' #pap
    print("PaP")
else:
    print("That was not one of the choices")

I'm only getting the "else" case returned, why might that be??

Upvotes: 1

Views: 344

Answers (6)

Blckknght
Blckknght

Reputation: 104712

While the other answers correctly identify the reason you're getting the else block in your current code, I want to suggest an alternative implementation that is a bit more "Pythonic". Rather than a bunch of nested if/elif statements, use a dictionary lookup, which can support arbitrary keys (including perhaps more meaningful ones than integers):

book_urls = {'cornwall': 'http://www.gutenberg.org/cache/epub/9878/pg9878.txt',
             'holmes': 'http://www.gutenberg.org/cache/epub/1661/pg1661.txt',
             'p and p': 'http://www.gutenberg.org/cache/epub/1342/pg1342.txt'}

print("For 'The Survey of Cornwall,' type 'cornwall'")
print("For 'The Adventures of Sherlock Holmes,' type 'holmes'")
print("For 'Pride and Prejudice,' type 'p and p'")

choice = input("Which do you choose?") # no conversion, we want a string!

try:
    url = book_urls[choice]
except KeyError:
    print("That was not one of the choices")
    url = None

You could make the whole thing data-driven if you wanted, with the book names and urls being provided as an argument to a function that would ask the user to pick one (without knowing what they were ahead of time).

Upvotes: 1

TeknasVaruas
TeknasVaruas

Reputation: 1510

input() returns a string type. So, you need to convert your input into an integer using int(), or else you can compare the inputs to characters instead of integers, like '1', '2'.

Upvotes: 1

PaulMcG
PaulMcG

Reputation: 63709

I'm guessing you are using Python 3, in which input behaves like raw_input did in Python 2, that is, it returns the input value as a string. In Python, '1' does not equal 1. You'll have to convert the input string to an int using n = int(n), then go through your succession of elifs.

Upvotes: 1

Ashwini Chaudhary
Ashwini Chaudhary

Reputation: 250911

input() returns a string in py3x. So, you need to convert it to int first.

n = int(input("Which do you choose?"))

Demo:

>>> '1' == 1
False
>>> int('1') == 1
True

Upvotes: 4

Mike Pelley
Mike Pelley

Reputation: 3116

input() returns a string, but you are comparing it to integers. You can convert the result from input to an integer with the int() function.

Upvotes: 3

Bear
Bear

Reputation: 516

You should convert the input with int() n = input("Which do you choose?") should be n = int(input("Which do you choose?")) This is due to the fact that input returns strings for all input since it should almost always work.

Upvotes: 1

Related Questions