[Tutor] Input() is not working as expected in Python 3.1

Dave Angel davea at ieee.org
Mon Feb 15 16:03:45 CET 2010


Yaraslau Shanhin wrote:
> Hello All,
>
> I am working with Python tutorial in wiki and one of the exercises is as
> follows:
>
> Ask the user for a string, and then for a number. Print out that string,
> that many times. (For example, if the string is hello and the number is 3 you
> should print out hellohellohello.)
>
> Solution for this exercise is:
>
> text = str(raw_input("Type in some text: "))
> number = int(raw_input("How many times should it be printed? "))print
> (text * number)
>
>
>
> Since in Python raw_input() function was renamed to input() according
> to PEP 3111 <http://www.python.org/dev/peps/pep-3111/> I have
> respectively updated this code to:
>
>
> text = str(input("Type in some text: "))
> number = int(input("How many times should it be printed? "))print
> (text * number)
>
>
>
> However when I try to execute this code in Python 3.1 interpreter
> error message is generated:
>
>
> Type in some text: some
> How many times should it be printed? 3
> Traceback (most recent call last):
>   File "test4.py", line 2, in <module>
>     number = int(input("How many times should it be printed? "))
> ValueError: invalid literal for int() with base 10: 'How many times
> should it be printed? 3'
>
>
> Can you please advise me how to resolve this issue?
>
>   
When I correct for your missing newline, it works for me.  I don't know 
of any version of Python which would copy the prompt string into the 
result value of input or raw_input() function.

Try pasting the exact console session, rather than paraphrasing it.

DaveA



More information about the Tutor mailing list