Why does this user input function fail?

Hi - I’m new to Python and am working through the lessons in “Learn Python”. Just for fun I tried to add user input for some of them but the code is failing - reducing it to its simplest form, this code fails:

name = input("Enter your name: ")
print(name)

If I enter an integer it works, but if I enter a string it fails and I don’t really understand why. My understanding is that the “input()” function returns a string, but that doesn’t seem to be the case here…help!

thanks in advance,
rich

Have you checked which version of python you have as the older versions have different commands?

This is in Codeacademy’s editor. I assume it’s 3 something, but don’t really know.

Could you try further explaining it to clarify?

Hello!

Can you send a link to the exercise and the specific error message you’re getting? Some Codecademy lessons/environments aren’t geared to receive user input so throws an error if you try, not sure if that’s the case here, but it would be nice to rule it out if it’s not.

At the top of the code in the editor:

import sys
print (sys.version)

Then run the code. It will give the version.

One suspects Python 2, so use, raw_input() instead of input().

1 Like

Thank you! That’s interesting: Code editor says v3.6.9
But I’ve also installed PyCharm and it fails using v2, but works if I run it with v3.10.

Clearly there’s a lot I don’t know aobut Python yet (and about CodeAcademy’s interface) - I suppose this kind of thing will get covered later in the lessons.

Given it works on Python 3 locally, but not on the Codecademy editor, the most likely thing is that the specific Codecademy environment you’re trying to run the code in simply doesn’t support user input, a link would help confirm that :slight_smile:

Your code should work, given it is version 3+, the note by @notlyall, notwithstanding. Chances are either the support is not present, or, there is a short timeout. When you press Run, be sure to click on the prompt in the console, type quickly and Enter.

2 Likes

Thank you all for the responses - your input clarifies this for me a bit. I’m gonna chalk this up to version issues and oddities of CodeAcademy’s interface. I’ll just keep plugging away at the lessons and hopefully this will all become clearer as I go.

1 Like