1

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??

1
  • 3
    also, there's a quote missing after "holmes. Just letting you know. Commented May 12, 2013 at 4:28

6 Answers 6

4

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
Sign up to request clarification or add additional context in comments.

5 Comments

cool, thanks guys, gonna pick this one as the answer when the timer expires!
Also, if you just want a string (which you can then convert to an int), you should be using raw_input. input is unsafe, as it runs eval on what the user types.
@sapi OP is usong py3x not py2x, and raw_input has been renamed to input in py3x.
@AshwiniChaudhary - my mistake. I didn't notice that from the OP.
@sapi - actually, I don't think the OP ever actually said, but if he/she were running Py2, then the code would have run okay as-is, as the Py2 behavior of input was to also do an implicit eval on the string before returning it.
3

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.

Comments

1

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.

Comments

1

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.

Comments

1

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'.

Comments

1

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).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.